Saving nsmanagedobject context not saving

Discussion in 'iPhone/iPad Programming' started by RookieAppler, Jan 24, 2013.

  1. RookieAppler, Jan 24, 2013
    Last edited by a moderator: Jan 24, 2013

    macrumors member

    Mar 15, 2012
    		NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] init];
    		[ctx setUndoManager:nil];
    		ITMAppDelegate *appDel = (ITMAppDelegate *)[[UIApplication sharedApplication] delegate];
    		[ctx setPersistentStoreCoordinator: [appDel persistentStoreCoordinator]];
    		//self.managedObjectContext = appDelegate.managedObjectContext;
    		ITMDistinctCustomers *distinctCustomer = (ITMDistinctCustomers *)[NSEntityDescription
    [self.distinctCustomerArray addObject:distinctCustomer];
    NSError *error = nil;
    	if (![ctx save:&error])
    		NSLog(@"Failed to save distinct customer");
    I make an NSMutableArray of distinctCustomers and try to save them but it wont save. I put only this entity here. I have 10 other entities and neither saves. Please help me out. Thanks.
  2. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    How do you know it won't save? What debugging have you done? What, if any, has it shown?
  3. RookieAppler, Jan 25, 2013
    Last edited: Jan 25, 2013

    thread starter macrumors member

    Mar 15, 2012
    @dejo.I have this tool called SQLite Manager, a firefox addon.In that tool I went to the ITMDistinctCustomers table and did "select * from ITMDistinctCustomers "and it has nothing in there.
  4. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Did you check what the NSError returns?
  5. macrumors 68030


    Jan 19, 2006
    I wonder if the problem is that you're not passing the baton. You seem to be creating a new baton out of thin air in the hands of the second runner and trying to bless it as the official baton.

    A quick read of the doc's and sure you can attach multiple MOC's to the same PersistentStoreCoordinator but the store uniques between the contexts. I sort of wonder if the uniquing is obscuring the created objects. Are there and objects in the file?

    If your going to the appDelegate to get the PersistentStoreCoordinator anyway why not just ask it for the ManagedObjectContext instead?

Share This Page