一次有多个RKObjectManager(RestKit) [英] More than one RKObjectManager at a time (RestKit)

查看:137
本文介绍了一次有多个RKObjectManager(RestKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试RestKit并且需要访问不同的BaseUrls并且有时也同时从不同的地方访问具有相同baseUrl的web服务,最后我还需要在同一个控制器中访问具有不同ressourcePath的相同baseUrl。

I am testing out RestKit and need to access different BaseUrls and also sometimes access a web service with the same baseUrl from different places "at once", lastly I also need to access the same baseUrl with different ressourcePaths in the same controller.

在我的app委托中,我设置了这样的RKObjectManager单例。

In my app delegate I set up the RKObjectManager singleton like this.

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:kBaseUrl];
[objectManager registerClass:[EntityClass1 class] forElementNamed:@"element1"];
[objectManager registerClass:[EntityClass2 class] forElementNamed:@"element2"];
.
.
.
etc.

单例方法真的很容易使用,但我不能弄清楚如何分离不同的Web服务调用。

The singleton approach is really easy to work with, I however can't figure out how to separate the different web service calls.

在实现RKObjectLoaderDelegate的MyViewController中,我将有两种方法:

In MyViewController, which implement the RKObjectLoaderDelegate, I will have the two methods:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects         {
    //stuff with result
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
    //stuff with error    
}

当MyViewController使用一个RKObjectManager单例访问一个带有一个baseUrl的ressourcePath时,这不会导致任何问题。

This causes no problems when MyViewController uses one RKObjectManager singleton to access one ressourcePath with one baseUrl.

如果我以这种方式启动不同的请求:

If I start different requests in this way:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:FLICKRPath delegate:self]
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:FOURSQUAREPath delegate:self]

依此类推,在同一个MyController中,我的问题是FLICKRPath和FOURSQUAREPath of当然有不同的baseUrl,但是RKObjectManager只有一个?

and so on, within the same MyController, my problem is that FLICKRPath and FOURSQUAREPath of course has different baseUrl, but the RKObjectManager only has one?

如果我使用它并且可以有不同的RKObjectManagers,则会出现另一个问题。
委托方法didLoadObjects和didFailWithError将从两个RKObjectManagers接收结果,我看不到任何其他方式来区分它们而不是它们的baseUrls。可能将每个返回值与baseUrl进行比较,更糟糕的是,委托方法中的ressourcePath根本不会吸引我。

If I get this working and can have different RKObjectManagers another problem arises. The delegate methods didLoadObjects and didFailWithError will receive results from both RKObjectManagers and I can't see any other way to tell them apart than from their baseUrls. Potentially comparing each return value with a baseUrl and, even worse, a ressourcePath, in the delegate method does not appeal to me at all.

如果我有不同的RKObjectManagers我猜我可以传递给他们不同的委托,并构建专门用于处理来自不同baseUrls和ressourcePaths的返回值的类。这意味着我必须在MyController和RestKit之上构建另一个抽象,这看起来也很混乱。

If I have different RKObjectManagers I guess I could pass them different delegates and build classes dedicated to deal with the return values from different baseUrls and ressourcePaths. This would mean I had to build yet another abstraction on top of MyController and RestKit, which also seems messy.

我有一种强烈的感觉我会在错误中做到这一点方式,RestKit源非常令人印象深刻,这表明我正在与框架作斗争。我真的很感激这个主题的一些最佳实践见解。我已经浏览了所有可以找到的资源和示例,但没有看到上述用例。它总是一个RKObjectManager,一个baseUrl和一个ressourcePath。

I have a strong feeling I am going about this in the wrong way, the RestKit source is very impressive which indicates that is me fighting the framework. I would really appreciate some best practice insights on the subject. I have been through all the resources and examples that I could find but have not seen the above use case. It is always one RKObjectManager, one baseUrl and one ressourcePath.

提前谢谢你。

推荐答案

由于尚未接受答案:使用 RestKit 使用多个对象管理器非常简单。

Since there is no accepted answer yet: using multiple object managers is quite simple using RestKit.

来自 Wiki 使用多个基本URL(和多个对象管理器):


你创建的第一个对象管理器将是默认使用的共享单例
RestKit。但是通过创建额外的对象管理器,
你可以根据需要从他们的BaseURL中提取,只需要一定要保留
这些新经理。

The first object manager you create will be the shared singleton RestKit uses by default. But by creating additional object managers, you can pull from their BaseURLs as needed, just be sure to retain these new managers.



RKObjectManager *flickrManager = 
    [RKObjectManager objectManagerWithBaseURL:flickrBaseUrl]; // <-- shared singleton
RKObjectManager *foursquareManager = 
    [[RKObjectManager objectManagerWithBaseURL:foursquareBaseUrl] retain]; // <-- you must retain every other instance.




根据您的应用程序,您可能希望将第二个对象设置为
经理在一个更容易接近的地方,比如
AppDelegate上的保留属性,因此很容易根据需要提取。
如果您需要区分
两个(或更多)对象管理器的结果,只需在
userData中为查询设置一个标识符。

Depending on your application, you may want to put this second object manager in a more accessible place, like a retained property on the AppDelegate, so that it's easy to pull from as needed. In the event that you need to differentiate between the results from your two (or more) object managers, simply set an identifier in the userData for the queries.



- (void)someAction(id)sender {
        // .......
        RKObjectLoader* loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/whatever" delegate:self];
        loader.userData = @"foursquare";
        // or do this, if you need a number instead of a string
        loader.userData = [NSNumber numberWithInt:1234];
        // .......
    }

//Then when the delegate comes back you can cast it into a string or number as appropriate:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
    // .......
    NSString* source = (NSString*) objectLoader.userData;
    // or, if you did the NSNumber instead:
    NSNumber* source = (NSNumber*) objectLoader.userData;
    // .......
}

这篇关于一次有多个RKObjectManager(RestKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆