RestKit iOS应用程序的在线和离线支持

17

我希望使用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"];

用户对象是为了支持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表中,其中123在ZIDENTIFIER列中。

我是否漏掉了某个步骤才能让它起作用?也许还需要处理另一个委托方法,或者在超时后命中缓存时调用该方法?或者我试图做一些RestKit默认情况下不支持的事情?

干杯。


你好,我正在尝试使用RestKit 0.20.1做同样的事情 - 有任何更新吗?你成功了吗?你勾选的答案似乎并没有回答你的问题...谢谢! - Diego
1
我将下面的答案标记为正确,因为我采用了Julian的建议,使用可达性类来检查我是在线还是离线。后来,我使用RestKit进行在线通信,并自己使用CoreData进行离线存储。 - Diego Barros
好的,谢谢!我想我会尝试将这个集中在appdelegate中 - 虽然为了做到这一点,我认为最好创建一个HTTP操作队列,然后每次需要同步时解析它(可能是定期的)。 - Diego
2个回答

6
你可以使用Reachability类来确定客户端是否离线。我经常在需要互联网连接的每个项目中使用这个很棒的类。
你只需将通知器启动到特定主机即可。在所有的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];
    }
}

我使用了这种方法,但是我要警告你一件事。通知中心会以“随机”的顺序传递通知。因此,一个 ViewController 会先收到它,另一个稍后收到,导致应用程序不一致。也许你会在两个通知之间运行:一个 ViewController 看到“在线”,另一个看到“离线”...我建议只有一个类,可能是你的 AppDelegate,注册,然后整个应用程序通过内部 API 检查这个类。现在,你所有的类都将拥有一致的视图。 - malaba
@malaba - 你说得完全正确!我也是从AppDelegate中管理可达性的。 - Julian F. Weinert
2
你可以注册 RKReachabilityDidChangeNotification 并使用 RKReachabilityObserver 类来使用 RestKit 的功能。 - malaba

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接