开发者

How to ensure a single instance of a core data entity

I have an iPhone application and I would like to maintain profile information of the user. I also have a list of friends.

The information fields contained in the profile are identical to that of a friend.

I have a core data entity for a friend.

Should I make a separate entity for the profile or just use the friend entity.

If I use a separate entity, how do I ensure there is only 1 instance of the profile.

If I use the friend e开发者_Go百科ntity, how do I make sure that the profile info does not show up when I fetch a list of friends for the friend table view.

Thanks in advance.


You should perhaps rethink your naming conventions to make it easier to visualise your object model.

There way I see it, you have an entity called 'Member' with all the properties you currently have, plus a many to many "isFriend" relationship to itself.

This way, any 'friend' can be a 'profile', and any 'profile' can be a 'friend'. It will also make it very straight forward for you to instantiate a 'Member' object and pull all of its 'friends'.

As for making sure there is only one instance of the profile, you will need a unique identifier for each instance - without having much of a background on what you are trying to achieve, I'd suggest you think about having a username or email address field which you will be able to use as parameter to perform a fetchRequest and see if you get any existing hits before allowing a member to register/create a new profile.


how about you make a single table with attributes like

Name (NSString)
...
//your desired attributes which are common to both
isFriend (BOOL)//to keep track of profile type 

in this way you don't need to make two entity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜