RestKit序列化对象快捷方式

时间:2012-11-20 14:16:56

标签: iphone objective-c ios cocoa restkit

有更短的方法吗?

iam使用一些谓词获取所有核心数据对象,然后使用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;
}

1 个答案:

答案 0 :(得分:0)

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

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