我想为需要在离线和离线状态下工作的应用程序使用RestKit。RestKit iOS应用程序的在线和离线支持
我可能会误解RestKit的工作方式,但我认为这是相当容易实现的。
在划痕试验iOS应用余设置的东西,如下:根据需要CoreData支持
// setup the client
NSString* URL = @"http://10.211.55.5:3000/api";
RKClient* client = [RKClient clientWithBaseURL:URL];
client.username = @"[email protected]";
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:@"https://stackoverflow.com/users/:identifier"];
用户对象被实现:
@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
我然后对服务器的呼叫ID为123来获得用户,在路径 “/ API /用户/ 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来做同样的事情 - 对此有何更新?你有这个工作吗?你检查的答案似乎并没有回答你的问题......谢谢! – Diego 2013-05-23 15:11:03
我检查了下面的答案是正确的,因为我采取了朱利安的建议,使用可达性类来检查我是在线还是离线。后来,我最终使用RestKit进行在线通信,并将CoreData本人用于离线存储。 – dbarros 2013-05-24 23:29:32
好的,谢谢!我想我会尝试在appdelegate中集中这一点 - 虽然为了做到这一点,我认为如果我创建一个HTTP操作队列,然后我每次需要同步解析(可能定期),会更好。 – Diego 2013-05-31 13:59:08