Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does this code use only updates into core data?
    primarykey
    data
    text
    <p>This is a SyncEngine from an RW tutorial. I need help understanding how only UPDATED records from the web are fetched and processed into Core Data.</p> <pre><code>- (void)processJSONDataRecordsIntoCoreData { NSManagedObjectContext *managedObjectContext = [[SDCoreDataController sharedInstance] backgroundManagedObjectContext]; // Iterate over all registered classes --- CHECK! for (NSString *className in self.registeredClassesToSync) { if (![self initialSyncComplete]) { NSDictionary *JSONDictionary = [self JSONDictionaryForClassWithName:className]; NSArray *records = [JSONDictionary objectForKey:@"results"]; for (NSDictionary *record in records) { [self newManagedObjectWithClassName:className forRecord:record]; } } else { NSArray *downloadedRecords = [self JSONDataRecordsForClass:className sortedByKey:@"objectId"]; if ([downloadedRecords lastObject]) { NSArray *storedRecords = [self managedObjectsForClass:className sortedByKey:@"objectId" usingArrayOfIds:[downloadedRecords valueForKey:@"objectId"] inArrayOfIds:YES]; int currentIndex = 0; //if dl count is &lt; current index, there is an updated object dl from the web for (NSDictionary *record in downloadedRecords) { NSManagedObject *storedManagedObject = nil; //Quick check to see if they indeed match, if they do, update the stored object with remote service objects if ([storedRecords count] &gt; currentIndex) { storedManagedObject = [storedRecords objectAtIndex:currentIndex]; } //Othwerwise its a new object and you need to create a new NSManagedObject to represent it in CDdb if ([[storedManagedObject valueForKey:@"objectId"] isEqualToString:[record valueForKey:@"objectId"]]) { [self updateManagedObject:[storedRecords objectAtIndex:currentIndex] withRecord:record]; } else { [self newManagedObjectWithClassName:className forRecord:record]; } currentIndex++; } } } // After all NSMO are created in your context, save it! [managedObjectContext performBlockAndWait:^{ NSError *error = nil; if (![managedObjectContext save:&amp;error]) { NSLog(@"Unable to save context for class %@", className); } }]; // Cleanup time [self deleteJSONDataRecordsForClassWithName:className]; [self executeSyncCompletedOperations]; } [self downloadDataForRegisteredObjects:NO]; } </code></pre> <p>From what I understand, on the first or initial sync, it fetches JSONDictionaryForClassWithName which reads the downloaded data from disk and creates a newManagedObjectWithClassName.</p> <p>My confusion is in the update else block. downloadedRecords is populated from JSONDataRecordsForClass which simply calls JSONDictionaryForClassWithName. Then it checks to see if there is at least 1 object in that array. If there is it does this:</p> <pre><code>NSArray *storedRecords = [self managedObjectsForClass:className sortedByKey:@"objectId" usingArrayOfIds:[downloadedRecords valueForKey:@"objectId"] inArrayOfIds:YES]; </code></pre> <p>This fetches all managedObjectsForClass:sortedByKey which is below:</p> <pre><code>- (NSArray *)managedObjectsForClass:(NSString *)className sortedByKey:(NSString *)key usingArrayOfIds:(NSArray *)idArray inArrayOfIds:(BOOL)inIds { __block NSArray *results = nil; NSManagedObjectContext *managedObjectContext = [[SDCoreDataController sharedInstance] backgroundManagedObjectContext]; NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:className]; NSPredicate *predicate; if (inIds) { predicate = [NSPredicate predicateWithFormat:@"objectId IN %@", idArray]; } else { predicate = [NSPredicate predicateWithFormat:@"NOT (objectId IN %@)", idArray]; } [fetchRequest setPredicate:predicate]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"objectId" ascending:YES]]]; [managedObjectContext performBlockAndWait:^{ NSError *error = nil; results = [managedObjectContext executeFetchRequest:fetchRequest error:&amp;error]; }]; return results; } </code></pre> <p>The next bit which compares the [storedRecords count] > currentIndex is confusing. Can someone please explain this? I think my confusion lies in what the managedObjectsForClass method does with the usingArraysOfIds &amp; inArrayOfIds.</p> <p>I would expect that at some point it gets the the updatedAt field from the downloaded records and compares it to the updatedAt field of the CoreData fetched records.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload