Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save ManagedObjectContext at regular intervals? Currently dropping 1/3 of objects added
    primarykey
    data
    text
    <p>I'm trying to save the MOC at intervals of 1000, however the below code just seems to miss some of them. This only saved 2/3 of the objects, the rest just seemed to disappear into the abyss. This seems like such a cludgy way to achieve this, so if anyone can suggest a better way I'd love to hear it. </p> <p>This method is also taking about 9 minutes to add 115,000 objects. Is there anything I can do to improve this? Thanks. </p> <pre><code>dispatch_async(backgroundDispatchQueue,{ NSManagedObjectContext *backgroundThreadContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType]; [backgroundThreadContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; [backgroundThreadContext setUndoManager:nil]; [products enumerateObjectsUsingBlock:^(id product, NSUInteger idx, BOOL *stop) { NSManagedObject* newProduct; newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Products" inManagedObjectContext:backgroundThreadContext]; [newProduct setValue:[product valueForKey:@"product_name"] forKey:@"name"]; [newProduct setValue:[product valueForKey:@"product_codes"] forKey:@"codes"]; if ([product valueForKey:@"information"] == (id)[NSNull null]){ // No information, NULL [newProduct setValue:@"" forKey:@"information"]; } else { NSString *information = [product valueForKey:@"information"]; [newProduct setValue:information forKey:@"information"]; } if ([product valueForKey:@"megaimportant"] == (id)[NSNull null]){ // No information, NULL [newProduct setValue:@"" forKey:@"megaimportant"]; } else { NSString *megaimportant = [product valueForKey:@"megaimportant"]; [newProduct setValue:megaimportant forKey:@"megaimportant"]; } if ((self.productDBCount % 1000) == 0){ NSLog(@"SAVE ME"); NSError *error; if(![backgroundThreadContext save:&amp;error]) { NSLog(@"There was a problem saving the context (add/update). With error: %@, and user info: %@", [error localizedDescription], [error userInfo]); } } else if ((self.productDBCount + 1) == self.totalCount){ NSLog(@"Final Save"); NSError *error; if(![backgroundThreadContext save:&amp;error]) { NSLog(@"There was a problem saving the context (add/update). With error: %@, and user info: %@", [error localizedDescription], [error userInfo]); } } dispatch_async(dispatch_get_main_queue(), ^ { self.productDBCount = self.productDBCount + 1; float progress = ((float)self.productDBCount / (float)self.totalCount); int percent = progress * 100.0f; self.downloadUpdateProgress.progress = progress; self.percentageComplete.text = [NSString stringWithFormat:@"%i", percent]; NSLog(@"Added / updated product %f // ProductDBCount: %i // Percentage progress: %i // Total Count: %i", progress, self.productDBCount, percent, self.totalCount); NSDate *currentProcessedDate = [NSDate date]; NSTimeInterval timeSinceStarted = [currentProcessedDate timeIntervalSinceDate:self.startProcessing]; NSInteger remainingProcesses = self.totalCount - self.productDBCount; float timePerProcess = timeSinceStarted / (float)self.productDBCount; float remainingTime = timePerProcess * (float)remainingProcesses; self.timeRemaining.text = [NSString stringWithFormat:@"ETA: %0.0f minutes %0.0f seconds", (((float)remainingTime - fmodf(remainingTime, 60.0f))/60), fmodf(remainingTime, 60.0f)]; if (self.productDBCount == self.totalCount){ [self updatesCompleted:[jsonArray valueForKey:@"last_updated"]]; } }); }]; }); </code></pre>
    singulars
    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