Friday, 15 January 2010

ios - Updating Core Data +iCloud model with the next App version -



ios - Updating Core Data +iCloud model with the next App version -

i have work on next version of application uses core info , icloud. icloud has been activated in current version using "modern" way:

if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:@"data_cfg" url:storeurl options:@{nspersistentstoreubiquitouscontentnamekey:@"thestore"} error:&error])

now need update store construction adding new entities, relationship , on...

i'm totally stuck because don't know improve way update model knowing icloud synchronised users data.

which best way perform update that? should aware of , should pay attending most? how migrate current data?

icloud backup doesn't matter, way here solution installing new sqlite model. if info within application can recreated/downloaded server, there great solution. don't need setup migration stack, there quick solution. trick delete old sqlite database , create new one.

here code used on application update. need add together in appdelegate.m

- (nspersistentstorecoordinator *)persistentstorecoordinator { nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"yourdatabase.sqlite"]; nsmanagedobjectmodel *managedobjectmodel = [self managedobjectmodel]; nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel: managedobjectmodel]; // check if have persistent store if ( [[nsfilemanager defaultmanager] fileexistsatpath: [storeurl path]] ) { nsdictionary *existingpersistentstoremetadata = [nspersistentstorecoordinator metadataforpersistentstoreoftype: nssqlitestoretype url: storeurl error: &error]; if ( !existingpersistentstoremetadata ) { // *really* bad has happened persistent store //[nsexception raise: nsinternalinconsistencyexception format: @"failed read metadata persistent store %@: %@", storeurl, error]; nslog(@"failed read metadata persistent store %@: %@", storeurl, error); } if ( ![managedobjectmodel isconfiguration: nil compatiblewithstoremetadata: existingpersistentstoremetadata] ) { if ( ![[nsfilemanager defaultmanager] removeitematurl: storeurl error: &error] ) nslog(@"*** not delete persistent store, %@", error); } // else existing persistent store compatible current model - nice! } // else no database file yet [_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]; homecoming _persistentstorecoordinator; }

this code covers issues

if existing database old one, deleting , setting new one. if there no database yet (user download newest version) creating new database. if existing database compatible new one, using that.

just alter @"yourdatabase.sqlite" sqlitedb filename , work fine.

if your'e info can't recreated (synced server), need setup migration stack here link apple's guide https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredataversioning/coredataversioning.pdf

ios core-data migration upgrade icloud

No comments:

Post a Comment