- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想将 RestKit 用于需要在线和离线工作的应用程序。
我可能误解了 RestKit 的工作原理,但我认为这(相当)容易实现。
在 iOS 应用程序的 scratch 测试中,我设置如下:
// 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
这是我的缓存。请注意,我没有费心去检查 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/users/123”:
User* user = [User object];
user.identifier = [NSNumber numberWithInt:123];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager getObject:user delegate:self];
这很好用。但是,当我断开 Mac 上的 wifi 时,上面的代码不会从 sqlite 数据库中检索用户。
我在委托(delegate)的 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。
我是否缺少使它正常工作的步骤?也许是另一个委托(delegate)方法需要在缓存命中后被处理或调用暂停?还是我正在尝试做一些你不一定会用 RestKit “开箱即用”的事情?
干杯。
最佳答案
您可以使用 Reachability Class 来确定您的客户端是否离线。我经常在每个需要互联网连接的项目中使用这个很棒的类(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];
}
}
关于objective-c - 对 RestKit iOS 应用程序的在线和离线支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509328/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!