0

I am having a problem with updating the core data. i am downloading the data in the background thread comparing with the identifier in core data and count but am having trouble to update now i want to check the identifier present in Core data with JSON Response and if id is present in the JSON Response leave it and if not present in JSON Response (That means That record has been removed in Server side)

Here in this code am checking id is present in core data or not and now i want to check the id is present in json or not to update the records

Any help will be great appreciate thanks in advance please check the code how am storing the data in to core data

for (int i = 0; i < [arrayData count]; i++) {
        NSNumber * idNum = [arrayData objectAtIndex:i][@"id"];
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Deal"];
        [request setPredicate:[NSPredicate predicateWithFormat:@"identifier == %@",idNum]];
        [request setFetchLimit:1];
        NSUInteger count = [_managedObjectContext countForFetchRequest:request error:&error];
        if (count == NSNotFound) {
            NSLog(@"ERROR");
        }else if (count == 0) {
            Deal * dealsEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Deal" inManagedObjectContext:_managedObjectContext];
            NSString * name  = [arrayData objectAtIndex:i][@"name"];
            dealsEntity.nameAttribute = name;
            dealsEntity.identifier = idNum;
            [appDelegate saveContext];
        }
        [self performSelectorOnMainThread:@selector(updateData:) withObject:_myArray waitUntilDone:YES];
    }

- (void) updateData:(NSArray *)yourData {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Deal" inManagedObjectContext:_managedObjectContext];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    [fetchRequest setEntity:entity];
    NSError *error;
    yourData = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.myArray = yourData;
    [listTableView reloadData];
}

I have tried this with updating or deleting the records from coredata

for (int d = 0; d < [_myArray count]; d++) {
        Deal * deal = (Deal*)_myArray[d];
        NSNumber * identifier = [deal identifier];
        if ([identifier isEqualToNumber:[[arrayData objectAtIndex:d] valueForKey:@"id"]] ) {
            NSLog(@"equal %d",d);
        } else {
            NSLog(@"Kill it ");
        }
    }

but here the problem is Coredata has 115 records when checking with json but json will have only 114 records and it returns crash

Thanks
  • 13
  • 7

1 Answers1

0

As you have the identifier into your database you just need to change your code little bit

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Deal" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"identifier == %@",idNum]];
[request setFetchLimit:1];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

// check the count 

if([results count] == 1) {
   // Update your coredata object
} else {
   // Create new object 
}
Retro
  • 3,985
  • 2
  • 17
  • 41
  • for example now core data having 100 records and for json side one record is deleted that means i have to delete the one record in core data – Thanks May 08 '15 at 11:16
  • How do you decide that you want to create new data row or want to delete it? – Retro May 08 '15 at 11:17
  • if([results count] == 1) { in this place you told me to update the data by checking the CoreData with JSON – Thanks May 08 '15 at 11:21
  • Yes, it means data is available into coredata with that id, you can't use the same JSON for adding and updating the database and deleting too. – Retro May 08 '15 at 11:23
  • so for this i have to create a fetch request and get id present in coredata and check with the Json right – Thanks May 08 '15 at 11:24
  • Yes, but instead of getting count, you should get the actual object so you can modify it or delete it as needed. – Retro May 08 '15 at 11:25
  • so you mean to get identifier instead of count or results – Thanks May 08 '15 at 11:26
  • I mean get the actual object with that id! – Retro May 08 '15 at 11:27
  • You can handle it however you like! – Retro May 08 '15 at 11:30
  • so how can i delete the record do i need to create one more for loop and check with the json – Thanks May 08 '15 at 11:32
  • in the place results count == 1 i do i need to create one more fetch request with deal entity and create the loop there and delete the records there – Thanks May 08 '15 at 11:36
  • there is simple logical thing which you are missing, just fetch all the row from coredata and check with your jason loop and add those which id is not available into your jason and delete them – Retro May 08 '15 at 11:38
  • I tried with this by adding duplicate row in my sqlite file so in the next run it has to find thad duplicate right but it is returning me array beyond 115 like this please see the updated code the last code.. – Thanks May 08 '15 at 11:41
  • JSON & Core Data have 100 records for example if 1 record is deleted in json then in this case for loop is breaking returning me crash beyond... – Thanks May 08 '15 at 11:42
  • You can do one thing, make the array of all the jason id and all the coredata object id and using the NSSet removeObjectsInArray you will get the array of not available object which you can remove from coredata – Retro May 08 '15 at 11:55
  • can you give me sample – Thanks May 08 '15 at 12:02
  • i got all core data identifiers in to an array and also json array is ready what i do now – Thanks May 08 '15 at 12:39
  • Go to this link and you can get common and uncommon id http://stackoverflow.com/questions/7246273/compare-two-arrays-and-get-the-common-items – Retro May 08 '15 at 12:41
  • its given me common id's – Thanks May 08 '15 at 12:49
  • core data have 116 records json have 115 records so it returned me 115 common records id's now how can i get than only record so that i can delete the record – Thanks May 08 '15 at 12:51
  • this was given me simple solution – Thanks May 08 '15 at 12:58
  • http://stackoverflow.com/questions/1649198/objective-c-how-to-compare-arrays-and-extract-the-difference – Thanks May 08 '15 at 12:58
  • can you help me doing with NSDate – Thanks May 08 '15 at 12:59
  • and now am getting the uncommon records how can i delete those records please help me with the coding – Thanks May 08 '15 at 13:01