0

I tried few core data example. So what I have right now, I am able to save data to my cstore and also able to retrieve it. My problem is like this I am fetching data from server and storing it. Next time when I fetch data from server I want to save only new data.

My save data and fetch data methods looks like:

-(NSArray *)getUserListFromDB {

    NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Subscribers" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entitydesc];

    NSError *error;
    NSArray *channelData = [context executeFetchRequest:request error:&error];
    return channelData;
}

-(void)saveUserslListToDB:(SubscriberChannelListDataModel *) subscriberChannelList {

    NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Subscribers" inManagedObjectContext:context];

    NSMutableArray *user = subscriberList;   
    for(int position = 0; position < channelList.count; position ++) {
        NSManagedObject *newUser = [[NSManagedObject alloc] initWithEntity:entitydesc insertIntoManagedObjectContext:context];

        UserDataModel *userData = [user objectAtIndex:position];

        [newUser setValue: userData.userName forKeyPath:@"userName”];
        [newUser setValue: userData.userId forKeyPath:@“userId”];        
        NSError *error;
        [context save:&error];
        NSLog(@"Data save ..");
    }
}

Is there any one already faced same problem and have good solution?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
nilkash
  • 7,408
  • 32
  • 99
  • 176

2 Answers2

1

Your goal can be achieved in different ways..it depends on how much effort you need to put and how "big" your data are. With big I means if you have a lot of data or not.

The start point is to use a guid attribute for your entity. This attribute is used to see if a certain item is alreay present in the store or not. The right way to achieve it is to follow Implementing Find-or-Create Efficiently section.

Concerning the solutions an easy way is to grab all data from the server and update the objects (with all attributes) that are already in the store and insert the others that are not there. This could work for small datasets. Anyway you should need to test performances with Instruments.

A more complex way is to use in conjunction with your guid attribute also an updateAt timestamp (type of NSDate). Take a look at How to Sync iPhone Core Data with web server, and then push to other devices?. In particular the second answer presents a pseudo-algorithm for your goal.

Note. I guess you need to perform operation in background in order to not block the UI.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
0

Here's a code sample from a recent project I did. I'm using Magical Record here for searching but I think you will get the idea of what you can do by looking at this and it also handles checking to see if the object has a different updatedAt timestamp.

Package *package = [Package MR_findFirstByAttribute:@"id" withValue:[packageDict[@"id"] stringValue]];
NSDate *updatedAt = [dateFormatter dateFromString:packageDict[@"updated_at"]];
if (!package) {
    // Object doesn't exist yet, create it
    package = [Package MR_createEntity];
    package.id = [packageDict[@"id"] stringValue];
} 

if (![package.updated_at isEqualToDate:updatedAt]) {
     // Object has been updated, assign values
}

The idea is to do the following:

  1. Try to fetch the object from Core Data.
  2. If it doesn't exist, create it and give it an id.
  3. If the object is new or has been updated, assign it values, if not loop to the next.
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
smyrgl
  • 864
  • 6
  • 12
  • It doesn't cause an explicit break but I have moved away from using it in more recent projects. – smyrgl May 07 '14 at 07:36