2012-03-01 52 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中,從託管對象存儲中獲取緩存數據的方法略有改變。 我沒有這臺機器的例子;但如果您再需要幫助(因爲這是一個相當古老的問題),請回復。

相關問題