iphone - Preloaded "Core data" database not showing the correct new data after updating the database -
i have predefined , preloaded database in project named database
, had define follow: in appdelegate
- (nspersistentstorecoordinator *)persistentstorecoordinator { if (__persistentstorecoordinator != nil) { return __persistentstorecoordinator; } nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writabledbpath = [documentsdirectory stringbyappendingpathcomponent:@"database.sqlite"]; nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"database.sqlite"]; //este es el que sirve!!! cree este // nslog(@"store url %@", storeurl); // put down default db if doesn't exist nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath:writabledbpath]) { nsstring *defaultstorepath = [[nsbundle mainbundle] pathforresource:@"database" oftype:@"sqlite"]; if (defaultstorepath) { [filemanager copyitematpath:defaultstorepath topath:writabledbpath error:null]; } } nserror *error = nil; __persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; if (![__persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } return __persistentstorecoordinator; }
and in class wanna call data database use:
-(nsarray *)sqlcall { appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nsentitydescription *entitydesc = [nsentitydescription entityforname:@"motherweekly" inmanagedobjectcontext:context]; nsfetchrequest *request = [[ nsfetchrequest alloc] init]; [request setfetchlimit:1]; [request setentity:entitydesc]; nsstring *a =[nsstring stringwithformat:@"%@==%d",@"id",pagenumber]; nspredicate *pred = [nspredicate predicatewithformat:a]; [request setpredicate:pred]; nserror *error; nsarray *objects = [context executefetchrequest:request error:&error]; [request release]; return objects ; }
the problem is: when update field in preloaded database , run on simulator or iphone data appear old 1 not updated one.
ps: had submitted app appstore before noticed problem, im gonna reject binary , upload new version working/updated database, please me
thanks in advance
clearly, store not deleted when have new updated one. therefore see old values.
if want delete store if there new one, check nsuserdefault
version or tells if update necessary. if yes, delete persistent store, copy new 1 , set user default new version.
however, user data lost then. there 2 solutions this:
1) can have 2 persistent stores, 1 "static" data , 1 user data. however, might bit complex.
2) alternatively, include information of changed data , update database (copy if not exist, i.e. if new installation). preserve user data well. in fact, if data needs updated not much, not seem make sense copy whole database again.
indeed, if database copied has less than, say, 10.000 records, copying core data persistent store might questionable. quick background insert @ startup instead reading kind of file.
Comments
Post a Comment