在addPropertyMapping上的RestKit EXC_BAD_ACCESS代码2

时间:2013-12-31 17:20:41

标签: ios objective-c restkit restkit-0.20

当我m adding relation to the RKEntityMapping`这样的对象时      

[mapping addPropertyMapping:[RKRelationshipMapping 
               relationshipMappingFromKeyPath:@"user"
               withMapping:[MappingProvider userMapping]]];
调试器显示信息

  语法上的

“线程1:EXC_BAD_ACCESS(code = 2)”:静态NSArray   *文件RestKit / Core Data / RKEntityMapping.m中的RKEntityIdentificationAttributeNamesForEntity(NSEntityDescription *实体)

See link here

我的实现 - 在AppDelgate中是RestKit设置:

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"sCookingDB.sqlite"];
NSLog(@"Setting up store at %@", path);
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path
                                                                     fromSeedDatabaseAtPath:nil
                                                                          withConfiguration:nil
                                                                                    options:[self optionsForSqliteStore]
                                                                                      error:&error];
if (! persistentStore) {
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[managedObjectStore createManagedObjectContexts];
[RKManagedObjectStore setDefaultStore:managedObjectStore];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; NSError *error = nil; BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error); if (! success) { RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error); } NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"sCookingDB.sqlite"]; NSLog(@"Setting up store at %@", path); NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error]; if (! persistentStore) { RKLogError(@"Failed adding persistent store at path '%@': %@", path, error); } [managedObjectStore createManagedObjectContexts]; [RKManagedObjectStore setDefaultStore:managedObjectStore];

在控制器中我有方法

-(void) loadProducts {
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping: [MappingProvider productMapping] method:RKRequestMethodAny pathPattern: nil keyPath: nil statusCodes:statusCodes];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/products"]];
    RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];

    operation.managedObjectContext = [[RKManagedObjectStore defaultStore] mainQueueManagedObjectContext];
    operation.managedObjectCache = [[RKManagedObjectStore defaultStore] managedObjectCache];
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
        self.products = result.array;
        NSLog(@"%@", self.products);
        //[self.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Failed with error: %@", [error localizedDescription]);
        [SVProgressHUD showErrorWithStatus:@"Request failed"];
    }];
    NSOperationQueue *operationQueue = [NSOperationQueue new];
    [operationQueue addOperation:operation];
}

映射[MappingProvider productMapping]是: -(void) loadProducts { NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping: [MappingProvider productMapping] method:RKRequestMethodAny pathPattern: nil keyPath: nil statusCodes:statusCodes]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/products"]]; RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; operation.managedObjectContext = [[RKManagedObjectStore defaultStore] mainQueueManagedObjectContext]; operation.managedObjectCache = [[RKManagedObjectStore defaultStore] managedObjectCache]; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) { self.products = result.array; NSLog(@"%@", self.products); //[self.tableView reloadData]; [SVProgressHUD dismiss]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Failed with error: %@", [error localizedDescription]); [SVProgressHUD showErrorWithStatus:@"Request failed"]; }]; NSOperationQueue *operationQueue = [NSOperationQueue new]; [operationQueue addOperation:operation]; }

当我删除“

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:[[Product class] description]
                                                   inManagedObjectStore:  [RKManagedObjectStore defaultStore]];
    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"_id": @"productId",
                                                  @"name" : @"name",
                                                  @"created": @"created",

                                                  }];
    mapping.identificationAttributes = @[ @"productId" ];
    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user"
                                                                            toKeyPath:@"user"
                                                                          withMapping:[MappingProvider userMapping]]];

    return mapping;
”行时,代码工作正常,但当然只能获得没有用户的产品。

模型entities

我不知道我是否做错了,是否是RestKit中的错误。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您已将MappingProvider类配置为在无限循环中运行。 userMapping方法调用productMapping,然后再次调用userMapping。对任一方法的任何调用都将触发此循环。发生错误访问错误是因为设备耗尽内存以进行分配,因为递归循环已经完成所有操作。

尝试重写映射,以便不存在此递归依赖性。映射关系的一方应该足够了。