Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does context saving work in Core Data
    text
    copied!<p>I have a data base in my app. I use core data. I need to refresh my local data base with information coming from the server. I want to reduce time of updating DB. Let's say i have a table with 3 fields "ID", "Name", "file". In the "file" attribute is saved some large binary data. Here is the code to update my DB.</p> <pre><code>NSEntityDescription *entity=[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context]; NSFetchRequest *fetch=[[NSFetchRequest alloc] init]; [fetch setEntity:entity]; NSPredicate *predicate=[NSPredicate predicateWithFormat:@"ID == %@", newObject.objectID]; [fetch setPredicate:predicate]; NSError *fetchError; NSArray *fetchedEntities = [managedObjectContext executeFetchRequest:fetch error:&amp;fetchError]; NSManagedObject *objectToUpdate = [fetchedEntities firstObject]; objectToupdate.name = newObject.name; BOOL isRecordUpdated = objectToupdate.isUpdated; // returns YES. NSError *saveError; [managedObjectContext save:&amp;saveError]; </code></pre> <p>I found a record with ID and changed its name then saved the context. Object is changed and I can be sure it is updated on the server. The question is how does the update work. Will it rewrite the whole record? I mean what happens with the "file" attribute is it also rewritten? If yes, then how to avoid rewriting of large fields that are not changed. </p> <p>One more case. What if I rewrite the name with a new equal String?</p> <pre><code>NSString *name = [NSString stringWithFormat:@"%@", objectToUpdate.name]; BOOL nameChanged = firstName != contact.firstName; // returns YES BOOL nameIsEqual = [firstName isEqualToString:contact.firstName]; //returns YES objectToUpdate.name = name; BOOL hasChanges = [NSManagedObjectContext.defaultContext hasChanges]; //returns YES </code></pre> <p>I didn't change the name content but i changed the name value. If i save the context will it rewrite the Data Base? The aim of this question is to understand how to efficiently work with core data. Should i write custom properties to compare contents of strings, or should i store large data in a distinct table? Thanks for reading so far and sorry for being verbose.</p>
 

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