Skip to main content
Question

Setting attribute fails after get from UserDefaults()

  • November 16, 2024
  • 3 replies
  • 117 views

Forum|alt.badge.img+4

I would like to set a custom attribute with a string I get from UserDefaults().

This will NOT set the custom attribute:

let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(["cam": cam])

This will set the custom attribute:

Purchases.shared.attribution.setAttributes(["cam": "abc"])
let cam = "abc"
Purchases.shared.attribution.setAttributes(["cam": cam])
let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(["cam": "abc"])
Purchases.shared.attribution.setAttributes(["cam": cam])

UserDefaults combined with setting the attribute through a variable will not work, except if the attribute is previously set using a literal string (last example).

What do I miss here?
Is it a bug?
Is it Swift or RC related?

This post has been closed for comments

3 replies

Forum|alt.badge.img+4
  • Author
  • Active Member
  • November 16, 2024

Addendum to the previous (edit timed out).

It appears that attribute creation is not possible using a variable. It must be a literal.

Changing an existing attribute works fine.

The workaround would be the following:

let cam = UserDefaults.standard.string(forKey: "cam")!
Purchases.shared.attribution.setAttributes(["cam": "---"]) // dummy value
Purchases.shared.attribution.setAttributes(["cam": cam]) // correct value

To me, this looks like a bug on RC side?


joan-cardona
RevenueCat Staff
Forum|alt.badge.img+6
  • RevenueCat Staff
  • November 22, 2024

Hi ​@geohei,

Once you set the attribute, can you try doing a `syncAttributesAndOfferingsIfNeeded()` afterwards and check that the attribute is sent correctly? Also, can you enable debug logs and see if there’s any error there?

 

Thanks!

 


Forum|alt.badge.img+4
  • Author
  • Active Member
  • November 23, 2024

1.
Purchases.shared.syncAttributesAndOfferingsIfNeeded { offerings, error in }
This will sync the attribute, even if it doesn't exist and if its value has a variable (!= literal string) as origin. That’s good, but not what I want. I'd like the sync when the app goes in background (as per RC docs). I consider .syncAttributesAndOfferingsIfNeeded() as a workaround, not a solution.

2.
Still, .setAttributes behaves differently whether the value of the attribute has a variable (UserDefaults in my example) or a literal as origin.

.setAttributes with variable as value of non-existing attribute "cam":
DEBUG: ℹ️ setting values for attributes: ["cam"]
.setAttributes with literal as value of non-existing attribute "cam":
DEBUG: ℹ️ setting values for attributes: ["cam"]
DEBUG: ℹ️ Attribute set locally: [SubscriberAttribute] key: cam value: cam setTime: 2024-11-23 11:59:01 +0000. It will be synced to the backend when the app backgrounds/foregrounds or when a purchase is made.

It’s somewhat hard to explain. I believe you need to test it yourself. It looks strongly like a bug in RC to me.