FlurrySDK中UserProperties.Add与UserProperties.Set的功能差异及正确使用方式咨询
Hey there! Let's break down the differences between Flurry.UserProperties.Add and Flurry.UserProperties.Set in Unity, and clarify the right way to use them based on your question.
Core Differences Between Add and Set
First, let's ground this in the official docs you shared, then translate that into practical use cases:
Flurry.UserProperties.Set:官方文档描述:Set方法用于设置并替换(若已存在)属性的值
Think of this as an "upsert" operation. If the property doesn't exist yet, it creates the property and assigns the value. If the property already exists, it completely overwrites the old value with the new one. This is made for single-value properties—things like a user'scurrentLevel,subscriptionStatus, orselectedLanguagewhere there should only be one definitive value at any time.Flurry.UserProperties.Add:官方文档描述:Add方法用于添加用户属性值,添加已存在于当前状态中的值不会产生效果且不会报错。
This is exclusively for multi-value properties—attributes where a user can have multiple unique values, likefavoriteGameGenresorunlockedFeatures. Each call adds a new value to the property's collection, and duplicates are silently ignored (no errors, no changes). It never overwrites existing values, only appends new unique ones.
Can I Just Use Add and Ignore Set?
Short answer: No, that's a bad idea. They solve entirely different problems:
- If you use
Addfor a single-value property (likecurrentLevel), you'll end up with a list of every level the user has ever reached, instead of their active current level. That's useless for tracking their current state. Addonly makes sense for properties where multiple values are meaningful. For any attribute that needs one clear, up-to-date value,Setis the correct tool.
Do I Need to "Define" Properties via Add First, Then Use Set?
Nope, that's a common misunderstanding. You don't need to pre-define properties in Flurry through SDK calls. Both Add and Set will automatically create the property in your Flurry dashboard if it doesn't exist already.
The only time you'd chain Add calls is when building out a multi-value property (like adding multiple favorite genres). For single-value properties, you can call Set directly from the start—no pre-definition required.
Quick Practical Examples
Using Set for Single-Value Properties
// Set initial user level (creates the property if it doesn't exist) Flurry.UserProperties.Set("currentLevel", 5); // Later, when the user levels up, overwrite the old value Flurry.UserProperties.Set("currentLevel", 6);
Using Add for Multi-Value Properties
// Add multiple unique favorite genres (duplicates are ignored) Flurry.UserProperties.Add("favoriteGenres", "RPG"); Flurry.UserProperties.Add("favoriteGenres", "Strategy"); Flurry.UserProperties.Add("favoriteGenres", "RPG"); // This call does nothing
内容的提问来源于stack exchange,提问作者user1579019

