ios - Why won't my model save to Core Data? -


i'm quite new objective-c , i'm trying mess around core data. have following app delegate interface , implementation:

#import <uikit/uikit.h>  @interface sdtappdelegate : uiresponder <uiapplicationdelegate>  @property (strong, nonatomic) uiwindow *window;  @property (nonatomic, strong) nsmanagedobjectcontext *managedobjectcontext; @property (nonatomic, strong) nsmanagedobjectmodel *managedobjectmodel; @property (nonatomic, strong) nspersistentstorecoordinator *persistentstorecoordinator;  - (void)savemanagedobjectcontext; - (nsmanagedobjectcontext *)setupmanagedobjectcontext; - (nsmanagedobjectmodel *)setupmanagedobjectmodel; - (nspersistentstorecoordinator *)setuppersistentstorecoordinator; - (nsurl *)applicationdocumentsdirectory;  @end 

implementation

#import "sdtappdelegate.h" #import "todoitem.h"  @implementation sdtappdelegate  - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {     // managed object context , associate app delegate     self.managedobjectcontext = [self setupmanagedobjectcontext];      return yes; }  - (void)applicationwillterminate:(uiapplication *)application {     [self savemanagedobjectcontext]; }  /**  * save moc!  */ - (void)savemanagedobjectcontext {     nserror *error = nil;      if (self.managedobjectcontext != nil)     {         if ([self.managedobjectcontext haschanges] && ![self.managedobjectcontext save:&error])         {             /*              needs replaced              */             nslog(@"unresolved error %@, %@", error, [error userinfo]);             abort();         }     } }  #pragma mark - core data stack  /**  returns managed object context application.  if context doesn't exist, created , bound persistent store coordinator application.  */ - (nsmanagedobjectcontext *)setupmanagedobjectcontext {     if (self.managedobjectcontext != nil)     {         return self.managedobjectcontext;     }      nspersistentstorecoordinator *coordinator = [self setuppersistentstorecoordinator];      if (coordinator != nil)     {         self.managedobjectcontext = [[nsmanagedobjectcontext alloc] init];         [self.managedobjectcontext setpersistentstorecoordinator:coordinator];     }      return self.managedobjectcontext; }  /**  * return managed object model. if doesn't exist, create it.  */ - (nsmanagedobjectmodel *)setupmanagedobjectmodel {     if (self.managedobjectmodel != nil)     {         return self.managedobjectmodel;     }      nsurl *modelurl = [[nsbundle mainbundle] urlforresource:@"model" withextension:@"momd"];      self.managedobjectmodel = [[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];      return self.managedobjectmodel; }  /**  * return persistent store coordinator. if doesn't exist, create it.  */ - (nspersistentstorecoordinator *)setuppersistentstorecoordinator {     if (self.persistentstorecoordinator != nil)     {         return self.persistentstorecoordinator;     }      nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"model.sqlite"];      nserror *error = nil;      self.persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self setupmanagedobjectmodel]];      if (![self.persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype                                                        configuration:nil                                                                  url:storeurl                                                              options:nil                                                                error:&error])     {         /*          needs replaced          */         nslog(@"unresolved error %@, %@", error, [error userinfo]);         abort();     }      return self.persistentstorecoordinator; }  #pragma mark - application's documents directory  /**  * return url application's documents directory.  */ - (nsurl *)applicationdocumentsdirectory {     return [[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject]; }  @end 

and in controller have following save new model:

// take managed object context app delegate sdtappdelegate *appdelegate = (sdtappdelegate *)[uiapplication sharedapplication].delegate; nsmanagedobjectcontext *context = appdelegate.managedobjectcontext;  // add new item todoitem *newitem = [nsentitydescription insertnewobjectforentityforname:@"todoitem" inmanagedobjectcontext:context]; newitem.item = self.textfield.text; newitem.completed = 0; 

no matter happens can't application save. shows fine in interface, when quit , restart data no longer there. i'm guessing wrong persistent storage?

you should save context in order save in persistent store.

nserror *error; [context save:&error]; 

hope helps. cheers


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -