Search code examples
iphoneioscore-dataios6

Creating a news feed with CoreData


I'm working on a project in which an object has a feed of stories. However, just like Facebook or any other social network, those feed objects can reference various object types (an event, status update, etc). What I'm trying to figure out is how to store this in CoreData.

Currently I have it set up so a Group entity has many Story objects. However where I'm getting stuck is the story object. Here's what I have currently:

Story
---------
objectId   (the ID of the object being referenced)
objectType (Event, Status, etc)
createdAt 
updatedAt
metaData <-- This is what I'm trying to figure out

An event may have data such as "attendeeCount" versus a status update just has "text". The two options I've been considering are:

  1. Store a serialized NSDictionary object to store the meta data, or
  2. Make a subclass entity

1 sounds like the more compelling solution to me, but I wanted to hear from someone who has more than a week's experience with CoreData :) I have no need to search the actual feed items currently, just sort them by date and display them.


Solution

  • You can create abstract entity Story with basic parameters that all Story objects have. Than you can create other concrete entities like event, status update with specific fields and set Parent entity parameter of these concrete entities to Story. In this way concrete entities inherit all fields from abstract Story entity.

    (You can set Abstract entity and Parent entity parameters in Utilities -> Data Model Inspector).

    As a result you would have clear and readable interfaces for your entities. Also you can fetch and display all your entities in tableView specifying "Story" entity in fetch request.

    Edit

    Yes, you can set to-many relationship from Group to Story. E.g.

    enter image description here

    And you can add Event and Status entities like this:

        Group *grp = [NSEntityDescription insertNewObjectForEntityForName:@"Group" inManagedObjectContext:self.managedObjectContext];
        grp.goupID = @(12);
    
        Event *event = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
        event.storyID = @(123);
    
        Status *status = [NSEntityDescription insertNewObjectForEntityForName:@"Status" inManagedObjectContext:self.managedObjectContext];
        status.storyID = @(1);
    
        [grp addStoriesObject:event];
        [grp addStoriesObject:status];
    
        NSLog(@"group stories %@", grp.stories);
    

    Log results:

    2013-07-03 23:14:03.706 crdtTest[85418:c07] group stories Relationship 'stories' on managed object (0x74b1f50) <Group: 0x74b1f50> (entity: Group; id: 0x74b1fa0 <x-coredata:///Group/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA42> ; data: {
        goupID = 12;
        stories =     (
            "0x816e720 <x-coredata:///Status/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA44>",
            "0x816d980 <x-coredata:///Event/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA43>"
        );
    }) with objects {(
        <Status: 0x816e6d0> (entity: Status; id: 0x816e720 <x-coredata:///Status/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA44> ; data: {
        group = "0x74b1fa0 <x-coredata:///Group/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA42>";
        storyID = 1;
    }),
        <Event: 0x816d930> (entity: Event; id: 0x816d980 <x-coredata:///Event/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA43> ; data: {
        eventDate = nil;
        group = "0x74b1fa0 <x-coredata:///Group/t119EA6E0-0B3B-42EB-AB33-1553CE34ABA42>";
        storyID = 123;
    })
    )}