Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update all records as quickly as possible?
    primarykey
    data
    text
    <p>I need to update all the records with the same value. And I don't want to do it with iteration over objects since that takes about 2 seconds for over 2000 objects. Basically I have all users locally and I receive the users id's from the server. I now want to set property 'requested' to false except for the users return from the server-</p> <p>How it is done now (which is totally wrong!):</p> <p>Iterating thru all users (about 2000 of them) and correcting their requested value.</p> <pre><code>NSFetchRequest * allUsers = [[NSFetchRequest alloc] init]; [allUsers setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:context]]; NSArray *users = [context executeFetchRequest:allUsers error:&amp;error]; NSArray *userIDs = [JSON objectForKey:@"users_ids"]; for (User *user in users) { if ([userIDs containsObject:user.userID]) { user.requested = [NSNumber numberWithBool:YES]; } else { user.requested = [NSNumber numberWithBool:NO]; } user.requested = [NSNumber numberWithBool:NO]; } [context save:nil]; </code></pre> <p>My idea how to do it:</p> <p>Set requested=false for every object with a sql-like one statement. Going thru id's returned by the server (20 on average) and setting requested=true for those.</p> <pre><code>// UPDATE users SET requested = false WHERE 1=1 NSArray *userIDs = [JSON objectForKey:@"users_ids"]; for (int i = 0; i &lt; [userIDs count]; i++) { User *user = [User userWithServerID:[userIDs[i] integerValue] usingManagedObjectContext:context]; user.requested = [NSNumber numberWithBool:YES]; } [context save:nil]; </code></pre> <p>So how could I update all the records with one "query" ? Or does anyone have any better idea how to implement this (more performance efficient way of doing the iteration perhaps) ?</p> <p>Putting this in the background thread is a solution (perhaps), but I would still like to optimise it first before putting it in the background thread.</p> <p>Thank you!</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.
 

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