ios - How to update all records as quickly as possible? -
i need update records same value. , don't want iteration on objects since takes 2 seconds on 2000 objects. have users locally , receive users id's server. want set property 'requested' false except users return server-
how done (which totally wrong!):
iterating thru users (about 2000 of them) , correcting requested value.
nsfetchrequest * allusers = [[nsfetchrequest alloc] init]; [allusers setentity:[nsentitydescription entityforname:@"user" inmanagedobjectcontext:context]]; nsarray *users = [context executefetchrequest:allusers error:&error]; nsarray *userids = [json objectforkey:@"users_ids"]; (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];
my idea how it:
set requested=false every object sql-like 1 statement. going thru id's returned server (20 on average) , setting requested=true those.
// update users set requested = false 1=1 nsarray *userids = [json objectforkey:@"users_ids"]; (int = 0; < [userids count]; i++) { user *user = [user userwithserverid:[userids[i] integervalue] usingmanagedobjectcontext:context]; user.requested = [nsnumber numberwithbool:yes]; } [context save:nil];
so how update records 1 "query" ? or have better idea how implement (more performance efficient way of doing iteration perhaps) ?
putting in background thread solution (perhaps), still optimise first before putting in background thread.
thank you!
you still have fetch objects can in 1 line.
[[fetchedresultscontroller fetchedobjects] makeobjectsperformselector:@selector(setyourvalue:) withobject:[nsnumber numberwithbool:yes]];
or cycle array
nsarray *objects = [context executefetchrequest:allrequest error:&error]; [objects setvalue:[nsnumber numberwithbool:yes] forkey:@"user_ids"]; //now save changes back. [context save:&error];
Comments
Post a Comment