1

有没有更短的方法来做到这一点?

我用一些谓词获取所有核心数据对象,然后使用 RestKit 和定义的映射将它们解析为 JSON……我仍然想使用 RestKit,但不能做得更简单吗?

- (NSArray *)serializedManagedObjectClass:(Class)managedObjectClass updatedSinceRevision:(long long)revision {

    NSArray * managedObjects = [managedObjectClass MR_findAllSortedBy:@"createdDate" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"revision > %lld", revision]];

    NSError * error = nil;

    NSMutableArray * serializedObjects = [[NSMutableArray alloc] initWithCapacity:[managedObjects count]];

    for (NSManagedObject * managedObject in managedObjects) {

        id object = [NSMutableDictionary new];

        RKObjectMappingOperation * op = [RKObjectMappingOperation mappingOperationFromObject:managedObject toObject:object withMapping:[self.objectManager.mappingProvider serializationMappingForClass:managedObjectClass]];
        BOOL success = [op performMapping:&error];

        if ((success) && (error == nil) && (object)) {
            [serializedObjects addObject:object];
        } else {
            ALog(@"Serialization problem with %@, possible error %@", managedObject, error);
        }

    }

    return serializedObjects;
}
4

1 回答 1

0

您不需要单独映射每个对象。只要您为该类设置了序列化映射,RestKit 就会在您尝试发送该对象的实例时处理序列化。看起来您已经为该类创建并分配了映射。

现在,只需将托管对象数组添加到要发送到服务器的参数字典中。RestKit 然后应该根据提供的映射映射每个对象。

于 2012-11-20T16:07:12.657 回答