开发者

How to update NSManagedObject?

I have this code:

-(void)didEditCard:(NSMutableArray*)theArray {
    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObject *newManagedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath];

    // If appropriate, configure the new managed object.
    [newManagedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"];
    [newManagedObject setValue:[theArray objectAtIndex:1] forKey:@"flipCard"];

}

I knew which managed object was touched in the table view. If t开发者_如何学编程he player touches on a cell a view comes up and he can edit the entries. If he clicks on done then the above method will be called. BTW nothing is nil. But if i call setValue i won't be updated in core data.

My goal is: I want to update a managed object which should update the core data file to or i want to remove the object from the core data file and then add the new one at the exact same place.

Can someone help me please?

edit:

i tried this: but it doesn't work: (it doesn't gets updated)

-(void)didEditCard:(NSMutableArray*)theArray {
    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObject *managedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath];

    // If appropriate, configure the new managed object.
    [managedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"];

    NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext];
    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


}


Seems quite late to answer, but for the few who are still facing this issue, the following instructions should helpful.

You may try refreshing your object by the following method provided by coredata ,

- (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag; 

or you may query the record again from the database and use it. This should resolve your issue.

Important point:

Open the sqlite from the sandbox (simulator working directory, if you are testing on simulator) and observe the changes you did are actually reflected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜