json - Loading SQLite with OSX does not write all data -
i have osx application reads json file , inserts rows sqlite database using core data. problem not every row in json file loaded though application reports rows written.
it's last few hundred rows or not written leads me believe i'm not closing/flushing last writes database.
i don't see function in examples or on site. here code:
@autoreleasepool { nsmanagedobjectcontext *context = managedobjectcontext(); nsarray *gemlist; // // ready read json file // nsfilehandle *fh = [nsfilehandle filehandleforreadingatpath:@"/jsontesting/i10_cm_to_i9_cm.json"]; if (fh == nil){ nslog(@"cant open"); return -1; } // // read in // nsdata *jsontext; nslog(@"start reading"); jsontext = [fh readdatatoendoffile]; nslog(@"done reading"); // // load nsdictionary // nserror *jsonerror; nsdictionary *jsondictionary = [nsjsonserialization jsonobjectwithdata:jsontext options:nsjsonreadingmutablecontainers error:&jsonerror]; nslog(@"name: %@",[jsondictionary objectforkey:@"crosswalkname"]); gemlist = [jsondictionary objectforkey:@"gem"] ; // // set core data // nsfetchrequest *fetchrequest = [[nsfetchrequest alloc]init]; nsentitydescription *entity = [nsentitydescription entityforname:@"universalgem" inmanagedobjectcontext:context]; [fetchrequest setentity:entity]; [fetchrequest setfetchbatchsize:20]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"codesetid" ascending:no]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors]; nsmanagedobject *newmanagedobject; // // loop through , update database // int recordswritten = 0 ; (nsdictionary *gem in gemlist) { newmanagedobject = [nsentitydescription insertnewobjectforentityforname:[entity name] inmanagedobjectcontext:context]; nslog(@"source code: %@",[gem objectforkey:@"sourcecode"]); [newmanagedobject setvalue:[gem objectforkey:@"approximateflag"] forkey:@"approximateflag"]; [newmanagedobject setvalue:[gem objectforkey:@"choicelist"] forkey:@"choicelist"]; [newmanagedobject setvalue:[gem objectforkey:@"combinationflag"] forkey:@"combinationflag"]; [newmanagedobject setvalue:[jsondictionary objectforkey:@"crosswalkname"] forkey:@"crosswalkname"]; [newmanagedobject setvalue:[gem objectforkey:@"nomapflag"] forkey:@"nomapflag"]; [newmanagedobject setvalue:[gem objectforkey:@"scenario"] forkey:@"scenario"]; [newmanagedobject setvalue:[gem objectforkey:@"sourcecode"] forkey:@"sourcecode" ]; [newmanagedobject setvalue:[gem objectforkey:@"sourcedescription"] forkey:@"sourcedescription"]; [newmanagedobject setvalue:[gem objectforkey:@"targetcode"] forkey:@"targetcode" ]; [newmanagedobject setvalue:[gem objectforkey:@"targetdescription"] forkey:@"targetdescription"]; // save managed object context nserror *error = nil; if (![context save:&error]) { nslog(@"error while saving %@", ([error localizeddescription] != nil) ? [error localizeddescription] : @"unknown error"); exit(1); } recordswritten++; } nslog(@"records written: %d", recordswritten); }
Comments
Post a Comment