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

    Joined:
    Mar 15, 2012
    #1
    Code:
    		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
    																	insertNewObjectForEntityForName:@"ITMDistinctCustomers"
    																		  inManagedObjectContext:ctx];
    distinctCustomer.name=@"Blah";
    ...
    [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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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

    Joined:
    Mar 15, 2012
    #3
    @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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Did you check what the NSError returns?
     
  5. macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #5
    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