objective c - Error saving data in Core Data (IOS) -


my program receives json data web service. next, program stores data in database using core data. if call save data after adding each entry, works, slowly. keeping 200 entries takes more 1 minute.

if execute saving once @ end – program throw exception.

- (void) onloadmessages:(nsobject*)object {       nsarray *messages = (nsarray*)object;       if (messages==nil) {           [self onerror:@"message array null"];           return;       }        nsdate *date = [nsdate date];       long = [date timeintervalsince1970];       boolean update = false;       for(int i=0; i<messages.count; i++) {           nsdictionary *m = messages[i];           message *msg = [[message alloc]initwithdictionary:m];           if ([self updatemessage:msg updatetime:now])               update = true;       }       if (update) {           nserror *error = nil;           // error throw here           if (![self.managedobjectcontext save:&error])               [self onerror2:error];       }   }    - (boolean) updatemessage:(message*) msg updatetime:(long)now {       nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init];       // edit entity name appropriate.       nsentitydescription *entity = [nsentitydescription entityforname:@"messages" inmanagedobjectcontext:self.managedobjectcontext];       [fetchrequest setentity:entity];        nsstring *s = [nsstring stringwithformat:@"%ld", msg.id];       nspredicate *pred = [nspredicate predicatewithformat:@"(id=%@)", s];       [fetchrequest setpredicate:pred];        nserror *error;       nsarray *object = [self.managedobjectcontext executefetchrequest:fetchrequest error:&error];        boolean result = false;       if (object==nil)           [self onerror2:error];       else {           nsmanagedobject *m;           if ([object count]==0) {               // insert new message               m = [nsentitydescription insertnewobjectforentityforname:@"messages"                                                 inmanagedobjectcontext:self.managedobjectcontext];               [m setvalue:[nsnumber numberwithlong:msg.id] forkey:@"id"];               [m setvalue:[nsnumber numberwithlong:msg.agancy.id] forkey:@"agancy"];               [m setvalue:msg.header forkey:@"header"];               [m setvalue:msg.keywords forkey:@"keywords"];               [m setvalue:[nsnumber numberwithlong:msg.indate] forkey:@"indate"];               [m setvalue:[nsnumber numberwithlong:now] forkey:@"updated"];               result = true;         } else {               // update message               m = [object objectatindex:0];               [m setvalue:[nsnumber numberwithlong:now] forkey:@"updated"];           }            // save context.           // long execution           /*nserror *error = nil;          if (![self.managedobjectcontext save:&error])              [self onerror2:error];*/        }       return result;   }   

help correct behavior of program.

with respect, alexander.

p.s. execution takes place in main thread.

field "id" table "messages" indexed.

i solve problem adding privateobjectcontext!

_privateobjectcontext = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype];

and replace managedobjectcontext on privateobjectcontext when updating/inserting data/


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -