ios - How to copy sqlite database when we clicked on a UIButton? -
i storing details coredata nsstring.
nsentitydescription *entitydesc=[nsentitydescription entityforname:@"addnewvehicle" inmanagedobjectcontext:self.managedobjectcontext]; nsmanagedobject *newobject=[[nsmanagedobject alloc]initwithentity:entitydesc insertintomanagedobjectcontext:self.managedobjectcontext]; nserror *error; nsstring *instr = [nsstring stringwithformat: @"%d", (int)x]; [newobject setvalue:instr forkey:@"slno"]; [newobject setvalue:txt_platename.text forkey:@"platenumber"]; [newobject setvalue:txt_vehiclename.text forkey:@"vname"]; [self.managedobjectcontext save:&error];
also have 2 buttons named backup button , restore button.when cilck on backup button, current core data must duplicate coredatabackup.sqlite. tried following way:-
- (ibaction)savedata:(id)sender { nsstring * databasename = @"coredata.sqlite"; nsstring * databasebackupname = @"coredatabackup.sqlite"; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error = nil; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdir = ([paths count] > 0) ? [paths objectatindex:0] : nil; nsstring *backuppath = [documentsdir stringbyappendingpathcomponent:databasebackupname]; nsstring *dbpath = [documentsdir stringbyappendingpathcomponent:databasename]; nsurl *tourl = [nsurl fileurlwithpath:backuppath]; if([filemanager fileexistsatpath:backuppath]) { nslog(@"checks backuppath present ?? success"); //get rid of old copy, 1 allowed [filemanager removeitematpath:backuppath error:&error]; } if([filemanager fileexistsatpath:dbpath]) { if ([filemanager copyitematpath:dbpath topath:backuppath error:&error]) { nslog(@"creates backup"); [tourl setresourcevalue:[nsnumber numberwithbool:yes] forkey:nsurlisexcludedfrombackupkey error:&error]; } else { nslog(@"failed create writable database file message '%@'.", [error localizeddescription]); } }}
result creates coredatabackup.sqlite file on documents folder.but not copying current coredata details new file. don't know how create coredata.please 1 give useful advice.thanks in advance
you should disable wal mode in persistentstorecoordinator method of appdelegate
nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; nsmutabledictionary *pragmaoptions = [nsmutabledictionary dictionary]; /*attetion: disable wal mode*/ [pragmaoptions setobject:@"delete" forkey:@"journal_mode"]; nsnumber *optionyes = [nsnumber numberwithbool:yes]; nsdictionary *options = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithbool:yes], nsmigratepersistentstoresautomaticallyoption, [nsnumber numberwithbool:yes], nsinfermappingmodelautomaticallyoption, pragmaoptions, nssqlitepragmasoption, optionyes,nsmigratepersistentstoresautomaticallyoption ,nil]; if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:options error:&error]) { abort(); }
Comments
Post a Comment