2012-03-01 67 views
17

我想为需要在离线和离线状态下工作的应用程序使用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“开箱即用”的东西?

干杯。

+0

喜,我正在尝试使用RestKit 0.20.1来做同样的事情 - 对此有何更新?你有这个工作吗?你检查的答案似乎并没有回答你的问题......谢谢! – Diego 2013-05-23 15:11:03

+1

我检查了下面的答案是正确的,因为我采取了朱利安的建议,使用可达性类来检查我是在线还是离线。后来,我最终使用RestKit进行在线通信,并将CoreData本人用于离线存储。 – dbarros 2013-05-24 23:29:32

+0

好的,谢谢!我想我会尝试在appdelegate中集中这一点 - 虽然为了做到这一点,我认为如果我创建一个HTTP操作队列,然后我每次需要同步解析(可能定期),会更好。 – Diego 2013-05-31 13:59:08

回答

6

您可能使用可达性类来确定您的客户端是否处于脱机状态。我经常在每个需要互联网连接的项目中使用这个优秀的课程。

您只需将通知程序启动到特定主机即可。在所有viewController中,您现在只需向NSNotificationCenter注册方法即可设置BOOL isOnline

做这个练习,你可以在你的应用程序中做出漂亮的东西,就像用平滑的“离线”消息覆盖应用程序一样。

https://gist.github.com/1182373

编辑

继承人从我的项目之一登录画面一个例子(对不起,这个代码量,但是这是我的完整的实现):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //// Some stuff //// 
    [[Reachability reachabilityWithHostname:@"apple.com"] startNotifier]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 
} 
- (void)reachabilityChanged:(NSNotification *)note 
{ 
    if ([[note object] isReachable]) { 
     CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; 
     [scale setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:0.0], nil]]; 
     [scale setDuration:0.3]; 
     [scale setRemovedOnCompletion:NO]; 

     [[offlineView layer] setTransform:CATransform3DMakeScale(0, 0, 1.0)]; 
     [[offlineView layer] addAnimation:scale forKey:@"scale"]; 
     [[offlineView layer] setTransform:CATransform3DIdentity]; 

     [UIView animateWithDuration:0.3 animations:^{ 
      [offlineView setAlpha:0]; 
     } completion:^(BOOL finished){ 
      if (finished) { 
       [offlineView removeFromSuperview]; 
      } 
     }]; 

     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; 
    } 
    else { 
     CGRect screenFrame = CGRectMake(0, 0, 320, 480); 

     offlineView = [[UIView alloc] initWithFrame:screenFrame]; 
     [offlineView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]]; 
     [offlineView setAlpha:0]; 

     offlineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)]; 
     [offlineLabel setFont:[UIFont fontWithName:@"Verdana" size:30.0]]; 
     [offlineLabel setBackgroundColor:[UIColor clearColor]]; 
     [offlineLabel setTextAlignment:UITextAlignmentCenter]; 
     [offlineLabel setTextColor:[UIColor whiteColor]]; 
     [offlineLabel setCenter:[offlineView center]]; 
     [offlineLabel setText:@"OFFLINE"]; 

     [offlineView addSubview:offlineLabel]; 
     [[self window] addSubview:offlineView]; 

     [UIView animateWithDuration:0.3 animations:^{ 
      [offlineView setAlpha:1.0]; 
     }]; 

     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES]; 
    } 
} 
+0

我用这种方法,但我警告你一件事。通知中心将以“随机”顺序传递通知。因此,一个ViewController会在稍后得到它,导致您的应用程序出现不一致。也许你会在两个notif之间运行:在ViewController上看到“在线”,另一个看到“离线”... 我建议只有一个类,也许你的AppDelegate,注册,然后通过一个内部API检查整个应用程序类。你现在对你的班级有一个一致的看法。 – malaba 2012-07-20 09:53:16

+0

@malaba - 你是完全正确的!我也管理AppDelegate的可达性, – 2012-07-20 10:46:03

+2

您可以注册'RKReachabilityDidChangeNotification',并使用RKReachabilityObserver类来使用RestKit的东西。 – malaba 2012-07-20 12:11:36

0

请检查RestKit的版本;在较新版本的RestKit中,从托管对象存储中获取缓存数据的方法略有改变。 我没有这台机器的例子;但如果您再需要帮助(因为这是一个相当古老的问题),请回复。

相关问题