RestKit / Core Data:远程删除的实体不会从Core Data中删除 [英] RestKit / Core Data: Remotely deleted entities get not removed from Core Data

查看:119
本文介绍了RestKit / Core Data:远程删除的实体不会从Core Data中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么远程删除的实体未从Core Data和数据存储中删除?在
开始设置断点 - (void)deleteCachedObjectsMissingFromResult:(RKObjectMappingResult *)result RbManagedObjectLoader中的
显示变量结果不包含任何内容。

Why do remotely deleted entities not removed from Core Data and the datastore? Setting a breakpoint at the beginning of - (void)deleteCachedObjectsMissingFromResult:(RKObjectMappingResult *)result in RKManagedObjectLoader shows up that the variable result does not contain anything.

我可以通过在RestKit委托中实现这个功能来解决这个问题 - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects,在我看来不干净的代码。 RestKit / Core Data应该自己这样做吗?无论如何,下面的实现将解决问题:

I could fix that problem by implementing this feature in the RestKit delegate - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects but that is kind of unclean code in my point of view. RestKit / Core Data should do that by itself?! Anyway, following implementation would solve the problem:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{

NSArray *allReservations = [Reservation findAll];  

for(Reservation *reservationRecord in allReservations) {
    if(![objects containsObject:reservationRecord]) {
        [[[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] deleteObject:reservationRecord];
    }
}
}

没有didLoadObjects的帮助?

any ideas to solve that problem without the help of didLoadObjects? Adding / updating existing entities works properly.

推荐答案

RestKit只会删除NSManagedObjectContext中的条目。您的方法只编辑NSManagedObjectContext中的对象,但不会将它们保存到objectStore。确保在添加/编辑/删除完成后保存对ObjectStore的更改。

RestKit will only delete the entries in the NSManagedObjectContext. Your method only edits the objects in the NSManagedObjectContext but never saves them to the objectStore. Make sure to save the changes to the ObjectStore after the adding/editing/deleting has been finished.

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    NSArray *allReservations = [Reservation findAll];  

    // Deleting each item in NSManagedObjectContext
    for(Reservation *reservationRecord in allReservations) {
        if(![objects containsObject:reservationRecord]) {
            [[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] deleteObject:reservationRecord];
        }
    }

    // Changes only exist in NSManagedObjectContext, delete them in the ObjectStore
    NSError *error = nil;
    if (![[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread] save:&error]) 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

这篇关于RestKit / Core Data:远程删除的实体不会从Core Data中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆