【发布时间】:2012-03-01 01:20:16
【问题描述】:
我想将 RestKit 用于需要同时在线和离线工作的应用程序。
我可能误解了 RestKit 的工作原理,但我认为这(相当)容易实现。
在一个临时测试 iOS 应用程序中,我进行了如下设置:
// setup the client
NSString* URL = @"http://10.211.55.5:3000/api";
RKClient* client = [RKClient clientWithBaseURL:URL];
client.username = @"me@email.com";
client.password = @"password";
// setup caching
client.cachePolicy = RKRequestCachePolicyLoadIfOffline | RKRequestCachePolicyLoadOnError | RKRequestCachePolicyTimeout;
client.requestCache.storagePolicy = RKRequestCacheStoragePolicyPermanently;
// setup managed object store
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:URL];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RestKitCoreDataTest.sqlite"];
// connect my cache implementation
MyCache* cache = [[MyCache alloc] init];
objectStore.managedObjectCache = cache;
objectManager.objectStore = objectStore;
// setup mapping
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[userMapping mapKeyPath:@"firstname" toAttribute:@"firstname"];
[userMapping mapKeyPath:@"surname" toAttribute:@"surname"];
userMapping.primaryKeyAttribute = @"identifier";
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@""];
// setup routes
RKObjectRouter* router = [objectManager router];
[router routeClass:[User class] toResourcePath:@"/users/:identifier"];
User 对象根据 CoreData 支持的需要实现:
@interface User : NSManagedObject
@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * surname;
@end
@implementation User
@dynamic identifier;
@dynamic email;
@dynamic firstname;
@dynamic surname;
@end
这里是 MyCache。请注意,我不会费心检查resourcePath,因为这只是为了尝试一下,反正我只有一条路。
@implementation MyCache
- (NSArray*)fetchRequestsForResourcePath:(NSString*)resourcePath
{
NSFetchRequest* fetchRequest = [User fetchRequest];
return [NSArray arrayWithObject:fetchRequest];
}
-(BOOL)shouldDeleteOrphanedObject:(NSManagedObject *)managedObject
{
return true;
}
@end
然后我调用服务器以获取路径为“/api/users/123”的 id 为 123 的用户:
User* user = [User object];
user.identifier = [NSNumber numberWithInt:123];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager getObject:user delegate:self];
这很好用。但是,当我在我的 Mac 上断开 wifi 时,上面的代码不会从 sqlite 数据库中检索用户。
我在委托的objectLoader:didFailWithError 中收到以下错误:
2012-03-01 11:44:09.402 RestKitCoreDataTest[1989:fb03] error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x6b89aa0 {NSErrorFailingURLStringKey=http://10.211.55.5:3000/api/users/123, NSErrorFailingURLKey=http://10.211.55.5:3000/api/users/123, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x6b81ac0 "The request timed out."}
我认为通过指定在超时时应使用缓存,使用:“RKRequestCachePolicyTimeout”,我希望从本地缓存中检索用户。
缓存确实包含 ID 为 123 的用户记录——在 ZUSER 表中,在 ZIDENTIFIER 列中为 123。
我是否缺少一个步骤来使其正常工作?也许另一个委托方法 需要处理,或者被调用,当缓存命中后 超时?还是我正在尝试做一些你不一定会用 RestKit “开箱即用”的事情?
干杯。
【问题讨论】:
-
嗨,我正在尝试对 RestKit 0.20.1 做同样的事情——这有什么更新吗?你得到这个工作了吗?您检查的答案似乎没有回答您的问题...谢谢!
-
我检查了下面的答案是否正确,因为我接受了 Julian 的建议,即使用可达性类来检查我是在线还是离线。后来我最终使用 RestKit 进行在线通信,并自己使用 CoreData 进行离线存储。
-
好的,谢谢!我想我会尝试将其集中在 appdelegate 中——尽管为了做到这一点,我认为最好创建一个 HTTP 操作队列,然后在每次需要同步时(可能定期)解析它。
标签: objective-c ios restkit