primaryKeyAttribute不工作Restkit / Core Data [英] primaryKeyAttribute not working Restkit/Core Data

查看:97
本文介绍了primaryKeyAttribute不工作Restkit / Core Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了框架restkit 0.9.3,并按照讨论板的例子。嗯,一切都很好,但是当我试图使用Core Data时,我的User NSManagedObject类是重复的,即使在声明他的primaryKeyAttribute(userID)之后。例如,当我向我的网络服务器发送登录请求时,我返回 {user:{id:1,username:teste,...}} ..但它似乎每次创建一个新的行itLoader:didLoadObjects。

I just installed the framework restkit 0.9.3 and followed the Discussion Board example. Well, everything just worked great, however when I tried to use Core Data my User NSManagedObject class is duplicating even after declaring his primaryKeyAttribute (userID). For example, when I send a login request to my web-server, I return {"user":{"id":1, "username":"teste", ...}} .. but it seems to create a new row every time it invoques objectLoader:didLoadObjects.

用户表:

>

示例代码:

〜AppDelegate.m didFinishLaunching b
$ b

~ AppDelegate.m didFinishLaunching

RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];    
userMapping.primaryKeyAttribute = @"userID";
userMapping.setDefaultValueForMissingAttributes = YES; // clear out any missing attributes (token on logout)
[userMapping mapKeyPathsToAttributes:
     @"id", @"userID",
     @"email", @"email",
     @"username", @"username",
     @"password", @"password",
     nil];

[objectManager.mappingProvider registerMapping:userMapping withRootKeyPath:@"user"];

〜User.m loginWithDelegate

- (void)loginWithDelegate:(NSObject<UserAuthenticationDelegate>*)delegate {
    _delegate = delegate;   
    [[RKObjectManager sharedManager] postObject:self delegate:self block:^(RKObjectLoader* loader) {
        loader.resourcePath = @"/login";
        loader.serializationMapping = [RKObjectMapping serializationMappingWithBlock:^(RKObjectMapping* mapping) {
            [mapping mapAttributes:@"username", @"password", nil];            
        }];
    }];
}

〜User.m didLoadObjects(RKObjectLoaderDelegate) / p>

~ User.m didLoadObjects (RKObjectLoaderDelegate)

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

    if ([objectLoader wasSentToResourcePath:@"/login"]) {
        [self loginWasSuccessful];
    }

    NSLog(@"number of user rows: %i", [User findAll].count);
}

我做错了什么?

推荐答案

您是否正确地实现了RKManagedObjectCache?对于调试,我只是返回nil并忘记了。稍后,我发现我也有重复。

Are you correctly implementing RKManagedObjectCache? For debugging I had it simply return nil and forgot about that. A little while later I found I had duplicates also.

缓存通过获取本地对象和与服务器返回的对象进行比较。任何不在服务器响应中的本地对象都将被删除。在早期版本中,它使用获取请求,但在较新版本中,必须手动执行请求并返回实际对象。

The cache works by fetching local objects and comparing with server returned objects. Any local objects that are not in the server response will be deleted. In earlier versions it used a fetch request but in newer versions you must manually perform the request and return actual objects.

如果返回nil,它认为此对象不在您的缓存中,并将添加一个重复。尝试实现此方法:

If you return nil, it thinks this object is not in your cache and will add a duplicate. Try implementing this method:

+ (NSManagedObject *)findInstanceOfEntity:(NSEntityDescription *)entity
              withPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
                                value:(id)primaryKeyValue
               inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext



<例如:

For example:

+ (NSManagedObject *)findInstanceOfEntity:(NSEntityDescription *)entity
              withPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
                                value:(id)primaryKeyValue
               inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {

    NSFetchRequest* request = [[NSFetchRequest alloc] init];
    [request setEntity: entity];
    [request setFetchLimit: 1];
    [request setPredicate:[NSPredicate predicateWithFormat:@"%K = %@", primaryKeyAttribute, primaryKeyValue]];

    NSArray *results = [NSManagedObject executeFetchRequest:request inContext: managedObjectContext];
    if ([results count] == 0)
    {
     return nil;
    }
    return [results objectAtIndex:0];
}

这篇关于primaryKeyAttribute不工作Restkit / Core Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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