在我的iOS应用程序中,我试图从(RESTful)服务器获取和发布数据.我认为RestKit和AFIncrementalStore都是不错的选择.但是,我还需要能够脱机保存数据并在应用程序联机时将其推送到服务器.
我试图了解哪个是一个更好的框架用于应用程序.
此外,从一些在线资源中,似乎在AFIncrementalStore上使用故障可能会导致处理ManagedContext(通常是mainThread)的线程在提供网络请求以获取属性时卡住.这是一个大问题吗?
PS:添加afnetworking标签,因为afincrementalstore标签不可用
我创建一个类别对象并保存它:
NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];
_category = (Category *)[NSEntityDescription
insertNewObjectForEntityForName:@"Category"
inManagedObjectContext:managedObjectContext];
NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
NSLog(@"error saving: %@",error);
}
Run Code Online (Sandbox Code Playgroud)
然后编辑类别对象的名称并再次保存.
_category.name = _nameTextField.text;
NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];
NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
NSLog(@"error saving: %@",error);
}
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
2013-01-12 17:53:11.862 instacat[7000:907] Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x2027b300 {NSAffectedObjectsErrorKey=(
"<Category: 0x1ed43cf0> (entity: Category; id: 0x1ed52970 <x-coredata://68E5D7B6-D461-4962-BC07-855349DB3263-7000-00000141BAB4C399/Category/tE8AB2F2E-C14C-4E93-8343-CC245B7726622> ; data: {\n categoryId = …
Run Code Online (Sandbox Code Playgroud) 我正在使用NSPrivateQueueConcurrencyType
并发类型运行冻结(死锁?)而不是NSMainQueueConcurrencyType
.
我的上下文初始化
_managedObjectContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
Run Code Online (Sandbox Code Playgroud)
麻烦的代码:
NSManagedObjectID *managedObjectID = [self managedObjectIDForEntity:entity
withParseObjectId:object.objectId];
managedObject = [context existingObjectWithID:managedObjectID error:error];
Run Code Online (Sandbox Code Playgroud)
回溯:
multithreading deadlock core-data objective-c afincrementalstore
你如何映射AFIncrementalStore
到Twitter API v1.1?
AFNetworking的核心数据持久性,完成正确
https://github.com/AFNetworking/AFIncrementalStore
REST API v1.1资源
https://dev.twitter.com/docs/api/1.1
- (id)representationOrArrayOfRepresentationsOfEntity:(NSEntityDescription *)entity
fromResponseObject:(id)responseObject;
- (NSDictionary *)representationsForRelationshipsFromRepresentation:(NSDictionary *)representation
ofEntity:(NSEntityDescription *)entity
fromResponse:(NSHTTPURLResponse *)response;
- (NSString *)resourceIdentifierForRepresentation:(NSDictionary *)representation
ofEntity:(NSEntityDescription *)entity
fromResponse:(NSHTTPURLResponse *)response;
- (NSDictionary *)attributesForRepresentation:(NSDictionary *)representation
ofEntity:(NSEntityDescription *)entity
fromResponse:(NSHTTPURLResponse *)response;
- (NSMutableURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
withContext:(NSManagedObjectContext *)context;
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
pathForObjectWithID:(NSManagedObjectID *)objectID
withContext:(NSManagedObjectContext *)context;
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
pathForRelationship:(NSRelationshipDescription *)relationship
forObjectWithID:(NSManagedObjectID *)objectID
withContext:(NSManagedObjectContext *)context;
Run Code Online (Sandbox Code Playgroud) 假设我使用了Core Data模型AFIncrementalStore
,并且我有多个REST API端点用于检索该模型的对象列表.我可以覆盖-requestForFetchRequest:withContext:
在AFHTTPClient
像这样:
- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
withContext:(NSManagedObjectContext *)context {
NSMutableURLRequest *mutableURLRequest = nil;
if ([fetchRequest.entityName isEqualToString:@"Post"]) {
mutableURLRequest = [self requestWithMethod:@"GET" path:@"/posts/foo" parameters:nil];
}
return mutableURLRequest;
}
Run Code Online (Sandbox Code Playgroud)
在这个片段中,我检索Post
对象/posts/foo
,但我还需要从中检索另一个集合/posts/bar
.
我怎样才能做到这一点?我看到的唯一解决方案是制作两个模型:一个用于foo
和一个用于bar
,但重复自己是蹩脚的,并且可能有更多的API端点Post
为我提供了我需要支持的对象.我还缺少一些其他方法吗?
我一直想知道如果我要将AFIncrementalStore和Magical Record结合起来,生活会怎样.我只是不知道如何将增量存储设置为魔法记录存储.有谁知道如何进行这样的操作?
core-data ×4
ios ×3
afnetworking ×2
deadlock ×1
ios5 ×1
ios7 ×1
objective-c ×1
restkit ×1
twitter ×1