- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在创建一个具有离线模式的任务应用程序。我几乎已经使用 RestKit 成功创建了在线模式。
在离线模式下,我将每个对象的 isSync
属性设置为 false
,这样当网络可用时,我可以使用 predicate 获取这些未同步的对象并发布它们到服务器。
不幸的是,我找不到使用 RestKit 一次性将这些对象的数组POST
到服务器的方法。
RestKit 支持吗?或者有没有更好的方法使用 RestKit 实现离线支持?
更新:
我找到了一个方法here ,创建具有 NSArray
属性的中间 Entity
(具有自己的映射),可以将其发布到服务器。
但是我有很多实体(笔记、任务、评论等)需要离线功能,我是否必须为每个原始实体创建一个额外的中间实体? (这样每个响应都可以在我的原始实体中正确映射)
更新 2:
访问后here ,我发现 RestKit 0.20.0 现在支持 POSTing 多个对象的数组。但是每当我发布一个对象数组时,都不会向服务器发送任何参数。我就是这样做的:
[DBTasks createTask:task attachments:nil completionHandler:^(DBTasks *task, NSError *error) {
isPosting = NO;
[self setLoadingState:NO];
if (!error) {
KLog(@"task is %@", task); // works perfect
[self.tasksArray insertObject:task atIndex:0];
[self reloadTasks];
} else {
KLog(@"error %@", error);
}
}];
这是我实际发送 POST 请求的方法:
(只考虑else
部分)
+ (void)createTask:(DBTasks *)task attachments:(NSArray *)attachments completionHandler:(void (^)(DBTasks *, NSError *))completionHandler {
if (attachments != nil && attachments.count > 0) {
NSMutableURLRequest *request =[[RKObjectManager sharedManager] multipartFormRequestWithObject:task
method:RKRequestMethodPOST
path:URL_TASKS
parameters:@{@"total_attachments": [NSNumber numberWithInt:attachments.count]}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int counter = 0;
for (NSDictionary *dic in attachments) {
[formData appendPartWithFileData:UIImageJPEGRepresentation([dic objectForKey:@"image"], 0.7)
name:[NSString stringWithFormat:@"attachment[%i]", counter]
fileName:[dic objectForKey:@"name"]
mimeType:@"image/jpg"];
counter++;
}
}];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request
managedObjectContext:[NSManagedObjectContext MR_defaultContext]
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
KLog(@"success");
completionHandler((DBTasks *)[mappingResult firstObject], nil);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
KLog(@"fail");
completionHandler(nil, error);
}];
[operation start];
}
// without attachment
else {
[[RKObjectManager sharedManager] postObject:task path:URL_TASKS parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
completionHandler((DBTasks *)[mappingResult firstObject], nil);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
completionHandler(nil, error);
}];
}
}
映射如下:
- (RKEntityMapping *)tasksMapping {
RKEntityMapping *tasksMapping = [RKEntityMapping mappingForEntityForName:@"DBTasks" inManagedObjectStore:objectManager.managedObjectStore];
tasksMapping.setDefaultValueForMissingAttributes = NO;
tasksMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"shouldBeDeleted = 1"];
[tasksMapping setModificationAttributeForName:@"updated_at"];
tasksMapping.identificationAttributes = @[@"id"];
[tasksMapping addAttributeMappingsFromArray:@[@"completed_at", @"created_at", @"due_date", @"id", @"note", @"private", @"send_email", @"status", @"title", @"type", @"updated_at", @"user_id", @"parent_id", @"total_attachments", @"url", @"from", @"total_comments"]];
[tasksMapping addAttributeMappingsFromDictionary:@{@"deleted": @"shouldBeDeleted"}];
[tasksMapping addRelationshipMappingWithSourceKeyPath:@"attachments" mapping:[self attachmentsMapping]];
[tasksMapping addRelationshipMappingWithSourceKeyPath:@"owner" mapping:[self contactsMapping]];
[tasksMapping addRelationshipMappingWithSourceKeyPath:@"additional_owners" mapping:[self contactsMapping]];
[tasksMapping addRelationshipMappingWithSourceKeyPath:@"tags" mapping:[self tagsMapping]];
return tasksMapping;
}
相同的映射用于 POST/PUT/DELETE,但调用 inverseMapping
方法。
现在,当我用数组调用 postObject
时:
[DBTasks createTasks:[NSArray arrayWithObjects:task, task, nil] attachments:nil completionHandler:^(NSArray *array, NSError *error) {
isPosting = NO;
[self setLoadingState:NO];
if (!error) {
KLog(@"Array of objects is %@", array);
[self.tasksArray insertObject:task atIndex:0];
[self reloadTasks];
} else {
KLog(@"error %@", error);
}
}];
与之前的方法差不多:
+ (void)createTasks:(NSArray *)tasksArray attachments:(NSArray *)attachments completionHandler:(void (^)(NSArray *, NSError *))completionHandler {
[[RKObjectManager sharedManager] postObject:tasksArray path:URL_TASKS parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
completionHandler(mappingResult.array, nil);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
completionHandler(nil, error);
}];
}
应用终止
2014-05-05 16:18:30.692 MyApp[8195:f03] D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: (
{
attachments = (
);
"completed_at" = "<null>";
"created_at" = "<null>";
deleted = 0;
"due_date" = "<null>";
from = "Stand-alone task";
id = "<null>";
note = "<null>";
owner = "<null>";
private = 0;
"send_email" = "<null>";
status = "<null>";
tags = (
);
title = "<null>";
"total_attachments" = 0;
"total_comments" = 0;
type = Task;
"updated_at" = "<null>";
url = "http://10.28.79.98:3000/workspace/tasks?open=task_";
"user_id" = 26894;
}
)
and targetObject: (null)
2014-05-05 16:18:30.693 MyApp[8195:f03] D restkit.object_mapping:RKMapperOperation.m:297 Found mappable collection at keyPath '': (
{
attachments = (
);
"completed_at" = "<null>";
"created_at" = "<null>";
deleted = 0;
"due_date" = "<null>";
from = "Stand-alone task";
id = "<null>";
note = "<null>";
owner = "<null>";
private = 0;
"send_email" = "<null>";
status = "<null>";
tags = (
);
title = "<null>";
"total_attachments" = 0;
"total_comments" = 0;
type = Task;
"updated_at" = "<null>";
url = "http://10.28.79.98:3000/workspace/tasks?open=task_";
"user_id" = 26894;
}
)
2014-05-05 16:18:30.694 MyApp[8195:f03] CoreData: error: Failed to call designated initializer on NSManagedObject class 'DBTasks'
2014-05-05 16:18:30.694 MyApp[8195:f03] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
attachments = (
);
"completed_at" = "<null>";
"created_at" = "<null>";
deleted = 0;
"due_date" = "<null>";
from = "Stand-alone task";
id = "<null>";
note = "<null>";
owner = "<null>";
private = 0;
"send_email" = "<null>";
status = "<null>";
tags = (
);
title = "<null>";
"total_attachments" = 0;
"total_comments" = 0;
type = Task;
"updated_at" = "<null>";
url = "http://10.28.79.98:3000/workspace/tasks?open=task_";
"user_id" = 26894;
} with mapping <RKEntityMapping:0x10598240 objectClass=DBTasks propertyMappings=(
"<RKAttributeMapping: 0x10599660 completed_at => completed_at>",
"<RKAttributeMapping: 0x10599670 created_at => created_at>",
"<RKAttributeMapping: 0x10599680 due_date => due_date>",
"<RKAttributeMapping: 0x10599690 id => id>",
"<RKAttributeMapping: 0x105996a0 note => note>",
"<RKAttributeMapping: 0x105996b0 private => private>",
"<RKAttributeMapping: 0x105996c0 send_email => send_email>",
"<RKAttributeMapping: 0x105996d0 status => status>",
"<RKAttributeMapping: 0x105996e0 title => title>",
"<RKAttributeMapping: 0x105996f0 type => type>",
"<RKAttributeMapping: 0x10599700 updated_at => updated_at>",
"<RKAttributeMapping: 0x10599710 user_id => user_id>",
"<RKAttributeMapping: 0x10599720 parent_id => parent_id>",
"<RKAttributeMapping: 0x10599730 total_attachments => total_attachments>",
"<RKAttributeMapping: 0x10599740 url => url>",
"<RKAttributeMapping: 0x10599750 from => from>",
"<RKAttributeMapping: 0x10599760 total_comments => total_comments>",
"<RKAttributeMapping: 0x10599870 deleted => shouldBeDeleted>",
"<RKRelationshipMapping: 0x1059b1c0 attachments => attachments>",
"<RKRelationshipMapping: 0x1059c5f0 owner => owner>",
"<RKRelationshipMapping: 0x1059dca0 additional_owners => additional_owners>",
"<RKRelationshipMapping: 0x105a1e00 tags => tags>"
)>
2014-05-05 16:18:30.695 MyApp[8195:f03] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
2014-05-05 16:18:30.696 MyApp[8195:340b] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'DBTasks': {
"additional_owners" = {
isPrimitive = 0;
keyValueCodingClass = NSSet;
name = "additional_owners";
};
attachments = {
isPrimitive = 0;
keyValueCodingClass = NSSet;
name = attachments;
};
"completed_at" = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = "completed_at";
};
"created_at" = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = "created_at";
};
"due_date" = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = "due_date";
};
from = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = from;
};
id = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = id;
};
note = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = note;
};
owner = {
isPrimitive = 0;
keyValueCodingClass = DBContacts;
name = owner;
};
"parent_id" = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = "parent_id";
};
private = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = private;
};
"send_email" = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = "send_email";
};
shouldBeDeleted = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = shouldBeDeleted;
};
status = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = status;
};
tags = {
isPrimitive = 0;
keyValueCodingClass = NSSet;
name = tags;
};
title = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = title;
};
topic = {
isPrimitive = 0;
keyValueCodingClass = DBTopics;
name = topic;
};
"total_attachments" = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = "total_attachments";
};
"total_comments" = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = "total_comments";
};
type = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = type;
};
"updated_at" = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = "updated_at";
};
url = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = url;
};
"user_id" = {
isPrimitive = 0;
keyValueCodingClass = NSNumber;
name = "user_id";
};
}
2014-05-05 16:18:30.698 MyApp[8195:340b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x034721e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02be48e5 objc_exception_throw + 44
2 CoreFoundation 0x034fbeb8 -[__NSDictionaryM setObject:forKey:] + 888
3 MyApp 0x002dad3a __61-[RKPropertyInspector(CoreData) propertyInspectionForEntity:]_block_invoke61 + 154
4 libdispatch.dylib 0x04ce17b8 _dispatch_call_block_and_release + 15
5 libdispatch.dylib 0x04cf64d0 _dispatch_client_callout + 14
6 libdispatch.dylib 0x04ce4047 _dispatch_queue_drain + 452
7 libdispatch.dylib 0x04ce3e42 _dispatch_queue_invoke + 128
8 libdispatch.dylib 0x04ce4de2 _dispatch_root_queue_drain + 78
9 libdispatch.dylib 0x04ce5127 _dispatch_worker_thread2 + 39
10 libsystem_pthread.dylib 0x05025dab _pthread_wqthread + 336
11 libsystem_pthread.dylib 0x05029cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
有什么建议我做错了吗?
最佳答案
问题与发送数组无关,这是由于您的核心数据堆栈设置所致。你的两个案例有很大的不同。有效的方法使用魔法记录中的默认上下文。一个不使用对象管理器及其关联的对象存储的对象 - 似乎没有配置。
从代码和报错看,单发任务不带附件应该也会失败。
检查您的核心数据堆栈创建以及与对象管理器的关联。
关于ios - RestKit 0.20 - POST 多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23380725/
我使用 RestKit 与 REST api 进行交互。对于某些操作,例如 HTTP PUT/POST/DELETE,我只关心响应状态代码(200 或 500 等),而不关心响应数据,即使 API 确
我在客户端和服务器端都有一个这样的响应对象: 客户端: //BaseResponse.h @interface BaseResponse : NSObject{ NSString *code;
我正在尝试将 RestKit 0.20 集成到我的项目中,例如,我使用 google rss 阅读器作为示例项目。 这是对 Xml 的响应 / /类别> & 这是我必须从中获取数据的 Xml。 RKO
我很难使用 CocoaPods 将 RestKit 安装到我的项目中。我收到未找到的错误。 我的 Pods.xcconfig 看起来像这样: GCC_PREPROCESSOR_DEFINITIONS
大多数 RESTful API 都会路由对资源的访问,例如 Rabbit 对象,如下所示: GET /rabbits <--- GET all rabbits.
问题 在 iOS 上完成 Restkit 的安装说明后,我收到 Apple Mach-o 链接器错误: 我的流程 我尝试按照此处的 Restkit 安装说明进行操作:https://github.co
我完全按照此处找到的这些步骤操作: http://www.raywenderlich.com/13097/intro-to-restkit-tutorial 但是....我得到了错误: 这可能是什么原
我正在尝试使用 RestKit 调用需要基本身份验证的端点。 RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[JSNCateg
我想用RestKit ,但我已经使用 Realm.io而不是 CoreData。 是否可以在 Realm.io 之上使用 RestKit? 最佳答案 你当然可以。从 RestKit 取回对象后: //
我没有找到任何方法来设置 restkit 0.20.0 的超时间隔。 任何人都可以帮助增加超时间隔。 谢谢 最佳答案 子类 RKHTTPRequestOperation 和实现方法 - (NSURLR
我正在尝试实现 RESTKit 0.20 操作队列,我在博客上读到 NSOperationQueue 也可用于创建操作队列。我想使用 RestKit 操作队列的 native 方法。 任何人都可以发布
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 8年前关闭。 Improve thi
我在这里做的事情很奇怪吗? 我的类别得到下载和映射, 产品也像日志记录所说的那样被下载和映射, 但是我的产品在每个分类下都是空的,谢谢! { "productsCategories" : [
我尝试了几乎所有的解决方案,但都不适合我。我在 Xcode 6 上收到此错误。我也更新了 cocoa pod ,也更新了 pod 文件。 安装更新后,我在下面添加了几行: [!] From now o
我正在尝试使用 Jenkins-CI 自动构建一个使用 Restkit 的 iOS 项目,显然,restkit 作为一个项目驻留在主项目中。该项目使用 XCode-IDE 成功构建。 但是当我使用 x
我在 Xcode 6.1 中开发项目。它工作正常,突然提示错误“未找到 Restkit/Restkit.h 文件”。从 $ 更改Architectures 后出现错误(ARCHS_STANDARD_3
我按照 github 上的说明进行操作页面但有问题(肯定是我:P)但是,您知道在 Xcode4 RestKit 中集成/使用的任何好的教程吗? 谢谢! 最佳答案 从此链接下载最新版本 http://r
我在尝试让 RestKit 构建和工作时遇到了麻烦。我收到可怕的“词法或预处理器问题:未找到 RestKit/RestKit.h 文件”消息。我使用 Xcode 4.2,我的项目适用于 iOS。 早些
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]]; PPPS
有没有办法使用对象管理器访问请求的成功块中的响应数据? [objectManager postObject:[User class] path:@"/users" parameters:nil suc
我是一名优秀的程序员,十分优秀!