我对Objective-C和XCode相当新,但是甚至比RestKit更新,尽管已经用Java编程了12年.
我有一个连接到RestKit的IOS应用程序和从休息服务器加载的操作仅在第一次工作,如果我回到导航控制器并再次尝试相同的操作,该loadObjectsAtResourcePath:delegate方法被调用,但没有任何委托方法得到调用(我实现所有这些只是为了记录它们发生时).
我觉得我已经错过了与Restkit一些基本的东西,也许它的异步性质,因为映射显然是做工精细的第一次,所以后续调用必须在异步队列中迷路.
我用谷歌搜索了这个,看到类似的人被类似的东西卡住了,但是尝试了所有这些并且似乎并没有什么不同.缓存被禁用,objectLoaderDidLoadUnexpectedResponse并且didFailWithError永远不会被调用,它会被丢弃到地板上.
代码来源:
我的XCode项目在这里上了github
执行加载调用的特定View Controller在这里(参见setUpRestKit方法)
调用顺序如下:
启用日志,这是我看到的模式,第一个调用完成时注释,第二个调用开始时等(为了清晰起见,时间戳被修剪):
ElasticSearchHeadIOS[25064:fb03] I restkit:RKLog.m:32 RestKit initialized...
ElasticSearchHeadIOS[25064:fb03] I restkit.support:RKCache.m:178 Invalidating cache at path: /Users/paulsmith/Library/Application Support/iPhone Simulator/5.1/Applications/BDB8B59F-8307-4F18-A4E7-C55849618D77/Library/Caches/RKClientRequestCache-localhost/SessionStore
ElasticSearchHeadIOS[25064:fb03] I restkit.network.reachability:RKReachabilityObserver.m:155 Reachability observer initialized with hostname localhost
ElasticSearchHeadIOS[25064:fb03] D restkit.network:RKClient.m:271 Reachability observer changed for client <RKClient: 0x80756e0>, suspending queue <RKRequestQueue: 0x8082600 name=(null) suspended=YES requestCount=0 loadingCount=0/5> until reachability …Run Code Online (Sandbox Code Playgroud) 当我尝试POST时RestKit,Rails控制台中有一个警告:
Started POST "/friends" for 127.0.0.1 at 2012-04-16 09:58:10 +0800
Processing by FriendsController#create as */*
Parameters: {"friend"=>{"myself_id"=>"m001", "friend_id"=>"f001"}}
WARNING: Can't verify CSRF token authenticity
(0.1ms) BEGIN
SQL (1.7ms) INSERT INTO `friends` (`friend_id`, `myself_id`) VALUES ('f001', 'm001')
(1.1ms) COMMIT
Redirected to http://127.0.0.1:3000/friends/8
Completed 302 Found in 6ms (ActiveRecord: 3.0ms)
Run Code Online (Sandbox Code Playgroud)
这是客户端代码:
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject: @"f001" forKey: @"friend_id"];
[attributes setObject: @"m001" forKey: @"myself_id"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:attributes forKey:@"friend"];
[[RKClient sharedClient] post:@"/friends" params:params delegate:self];
Run Code Online (Sandbox Code Playgroud)
我该如何摆脱警告?
在我的iOS应用程序中,我试图从(RESTful)服务器获取和发布数据.我认为RestKit和AFIncrementalStore都是不错的选择.但是,我还需要能够脱机保存数据并在应用程序联机时将其推送到服务器.
我试图了解哪个是一个更好的框架用于应用程序.
此外,从一些在线资源中,似乎在AFIncrementalStore上使用故障可能会导致处理ManagedContext(通常是mainThread)的线程在提供网络请求以获取属性时卡住.这是一个大问题吗?
PS:添加afnetworking标签,因为afincrementalstore标签不可用
我想使用RestKit并在同一个类中处理几个不同的请求,即在didLoadResponse:方法中.如何区分不同的请求?我如何知道哪个请求已完成?
我正在通过这个请求
RKClient *client = [RKClient sharedClient];
[client get:@"/....", method] delegate:self];
Run Code Online (Sandbox Code Playgroud)
然后,在委托方法中
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
if (???) // request which gets XY returned
...
else if (???) // request which gets YZ returned
...
}
Run Code Online (Sandbox Code Playgroud)
那可能吗?
我遇到了一个新的问题,我似乎无法找到解决方法......这是我的RestKit代码,遵循Twitter核心数据示例:
//
// RESTKIT
//
// restkit object manager
NSString *baseURL = @"http://test1.website.com";
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]];
// enable activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
// managed object model
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"AppName" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
// NSLog(@"managed object model: %@", managedObjectModel);
// managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// NSLog(@"managed object store: %@", managedObjectStore);
// persistent store
NSError *error;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppName.sqlite"];
NSPersistentStore …Run Code Online (Sandbox Code Playgroud) 我是iOS开发的新手,我在制作一个简单的Json POST请求时遇到了麻烦.我有一个包含用户和密码的NSDictionary,我想将这些值作为Json发送到服务器并获得响应.我没有使用restkit工作,但我无法弄清楚如何使用RestKit完成相同的工作,只是找不到我想要的好例子.
- (bool) login{
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setValue:self.email forKey:@"email"];
[params setValue:self.password forKey:@"password"];
NSMutableDictionary* rpcData = [[NSMutableDictionary alloc] init];
[rpcData setValue:@"2.0" forKey:@"jsonrpc"];
[rpcData setValue:@"authenticate" forKey:@"method"];
[rpcData setValue:@"" forKey:@"id"];
[rpcData setValue:params forKey:@"params"];
[[RKClient sharedClient] post:@"/api/rpc/" params:rpcData delegate:self];
return nil;
}
Run Code Online (Sandbox Code Playgroud)
服务器期待像这样的Json:
{
jsonrpc : '2.0',
method : 'authenticate', // method name goes here
params : { // params are method-specific
email : 'test@test.com',
password : 'secret'
},
id : 2 // The id can be anything, it …Run Code Online (Sandbox Code Playgroud) 问题摘要:
考虑一类SyncObject是KVC-符合如属性:time,someValue,lastChange,uuid.
考虑一个仅NSArray包含的实例SyncObject.
我需要将数组作为JSON数组提交给服务器.
如何使用RestKit使用HTTP POST将此数组提交到服务器?
示例数组:
[
{
"time": "14:45 10/21/2011",
"someValue": "15",
"lastChange": "14:45 10/21/2011",
"uuid": "0b07c510-f4c8-11e0-be50-0800200c9a66"
},
{
"time": "14:50 10/21/2011",
"someValue": "62",
"lastChange": "14:51 10/21/2011",
"uuid": "1a6d4480-f4c8-11e0-be50-0800200c9a66"
}
]
Run Code Online (Sandbox Code Playgroud)
细节
我有一个对象数组,我需要服务器作为JSON.在我看来,RestKit是最简单的方法:我试图避免将对象转换为一组NSDictionary对象,然后使用一些JSON编码器来获取JSON,我可以POST到服务器.
因此,创建了数组,并为存储在数组中的对象类设置了映射,我自然会尝试访问POST服务器.
RKObjectManager* mgr = [RKObjectManager objectManagerWithBaseURL:@"http://localhost/someweb/api/"];
mgr.serializationMIMEType = RKMIMETypeFormURLEncoded;
mgr.client.username = @"username";
mgr.client.password = @"password";
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapKeyPath: @"time" toAttribute:@"time" …Run Code Online (Sandbox Code Playgroud) 我在iOS应用程序中使用RestKit,我需要对某些HTTP错误代码进行特殊处理.如何在内部检查响应HTTP状态代码request:didFailLoadWithError:?userInfo字典中是否有一些条目NSError?
我在RKRequestDelegate文档中找不到任何内容.
这是委托方法的接口:
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud) 我想将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 …Run Code Online (Sandbox Code Playgroud) 所以我们目前有一个使用Django创建的网站.现在,我们想创建一个使用相同后端的原生iOS应用程序,因此我们不必重新编写整个代码.根据我的理解,有两种替代路线:
1)直接调用Django URL,然后调用一个函数.在该函数中,使用编码的JSON数据创建HTTPResponse并将其发回.
2)从Django服务器创建一个类似于Tastypie的REST服务.但是,除了对对象进行直接的GET调用之外,我没有看到我们如何在TastyPie的Django模型中调用自定义函数.我们甚至可以这样做吗?
我发现令人惊讶的是,没有很多关于使用现有后端(如Django或RoR)从iOS使用Web服务的信息.例如,我知道Instagram使用Django,但他们如何从iOS与他们的服务器进行通信?!
非常感谢!
restkit ×10
ios ×5
objective-c ×5
cocoa-touch ×2
ios5 ×2
afnetworking ×1
django ×1
http ×1
iphone ×1
json ×1
post ×1
restkit-0.20 ×1
tastypie ×1
xcode4 ×1