Die*_*ros 17 objective-c ios restkit
我想将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"];
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是MyCache.请注意,我不打算检查,resourcePath因为这只是为了尝试,我还是有一条路.
@implementation MyCache
- (NSArray*)fetchRequestsForResourcePath:(NSString*)resourcePath
{
NSFetchRequest* fetchRequest = [User fetchRequest];
return [NSArray arrayWithObject:fetchRequest];
}
-(BOOL)shouldDeleteOrphanedObject:(NSManagedObject *)managedObject
{
return true;
}
@end
Run Code Online (Sandbox Code Playgroud)
然后我调用服务器以获取ID为123的用户,路径为"/ api/users/123":
User* user = [User object];
user.identifier = [NSNumber numberWithInt:123];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager getObject:user delegate:self];
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,当我在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."}
Run Code Online (Sandbox Code Playgroud)
我认为,由于指定在超时时应该使用缓存,使用:"RKRequestCachePolicyTimeout",我原本期望从本地缓存中检索用户.
缓存确实包含ID为123的用户记录 - 在ZUSER表中,在ZIDENTIFIER列中包含123.
是否有一个我不想让这个工作的步骤?也许在超时后命中缓存时需要处理或调用的另一个委托方法?或者我是否正在尝试做一些不一定是用RestKit"开箱即用"的东西?
干杯.
您可以使用Reachability Class来确定客户端是否脱机.我经常在每个需要互联网连接的项目中使用这个伟大的课程.
您只需将通知程序启动到特定主机即可.在您的所有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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5232 次 |
| 最近记录: |