0

I need to store an activity feed in an iOS application. Activity feed items will have a payload field which can be one of many (and I really mean many) types of entities in the system.

What is a good way to implement this payload relationship field on the Activity entity in my CoreData model?

Is it possible to use the id data type, or maybe use an NSManagedObject type?

One way to workaround this maybe to just store CoreData's entityId as a string in a special field, but I'd rather avoid that if there is a better way.


Example:

For simplicity let's say we have a not-so-standard blogging model: User, Blog, BlogPost, Comment and the following activities may happen:

  • User may create a new blog.
  • User may publish a new blog post.
  • A blog can be commented on.
  • A comment maybe liked.
  • etc.

Each of these generate a new Activity item on the website which in turns have a related payload relation to the item that was modified or being acted on.

Now I need to download, translate and store these activity feed items from the website in my iPhone application... so how do I mimic this payload field since it maybe pointing to any possible entity?

In my real code, though, there are about 10+ types of entities that could be put into this payload field so I'm looking for a good approach here.

chakrit
  • 61,017
  • 25
  • 133
  • 162
  • Could you give us some example how the schema of your data model would look like? You want to store objects of different type in the same field of a managed object? – MrTJ Mar 14 '12 at 09:51
  • @MrTJ Yeah, many many different types. – chakrit Mar 14 '12 at 11:12

2 Answers2

1

If you don't need to search / query the fields of your objects of variable type, then I suggest to use NSCoder to convert them into a binary representation and store them in a BLOB field of your managed object. You might want to store some type information as well in an other field of the same managed object. On the other side if you need to search between these variable objects then you have to create a new managed object type (entity) for each object. See my answer also here: NSCoding VS Core data

Community
  • 1
  • 1
MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • I can't exactly "create a new managed object type for each object" though, since that'd mean using a giant STI scheme that'd really bog down the app. Not sure if that's what you mean? The NSCoding / store JSON structure approach in the linked Q seems like a more workable approach to me. – chakrit Mar 14 '12 at 12:01
  • 1
    I meant "new entity for each object class" just formulated badly :) But it's really not the way to go if you have TOO much classes. Then the only way I can suggest is to serialize your objects to NSCoding / JSON structure. – MrTJ Mar 14 '12 at 12:08
0

Only thing that you can use is NSManagedObject. So you have to create your model and your relation and create new file for Activity and payload that will be subclasses of NSManagedObject.

Take a look at Core Data Programing Guide . You will find your answers in there.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71