我可以在带有restkit的异步结果中使用块吗?

发布于 2024-12-11 02:04:15 字数 546 浏览 0 评论 0原文

--更新:我决定尝试一下 AFNetworking。尽管 RestKit 具有非常好的对象映射功能,但网络调用的设计方式给我们带来了一些困难。

我希望获得一些关于如何组织使用 RestKit 的项目的建议。

我有几个来自存储库类的 REST 调用,其结果被传递到控制器。例如,我在存储库类中有一个 getProfile 方法,该方法从我们的浏览视图控制器中调用。视图控制器被设置为获取配置文件调用的委托,而存储库类被设置为restkit 调用的委托。

问题是,如果浏览控制器发出多个获取配置文件请求,则很难区分哪个结果应发送到哪个委托函数,因为所有 Restkit 调用共享相同的委托方法 objectLoaderDidFinishLoading。然后我有 4 个委托,我必须匹配 4 个异步 Restkit 请求的结果。

有什么方法可以使用块,以便我可以在 asnynrhounous 结果返回时传递要执行的函数,以便我可以分配适当的委托?我看到的块支持允许在其余套件中发送请求之前使用块,但我有兴趣在返回异步结果时使用它。

检查结果或设置用户数据并调查委托与异步结果的替代方案似乎不可靠且庞大。

--UPDATE: I've decided to give AFNetworking a try. Even though RestKit has a really nice object mapping functionality, the way the networking calls were designed have made some things difficult for us.

I'm hoping for some advice on how to organize my project that's using RestKit.

I have several REST calls from a repository class and its results get passed to controllers. For example I have a getProfile method in the repository class that is getting called from our browse view controller. The view controller is set as the delegate to the get profile calls while the repository class is set to the delegate for the restkit calls.

The problem is if the browse controller makes several get profile requests, it is difficult to distinguish which result should go to which delegate function since all the restkit calls share the same delegate method objectLoaderDidFinishLoading. I then have 4 delegates that I have to match the results of the 4 asynchronous restkit requests.

Is there any way I can use blocks so that I can pass a function to execute as the asnynrhounous result comes back so that I can assign a proper delegate? The block support that I saw allowed a block to be used before the request was sent out in rest kit but I am interested in using it for when the asynchronous result is returned.

The alternative of examining the results or setting user data and sleuthing what delegate goes with what asynchronous results seems unreliable and bulky.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕烟庭风 2024-12-18 02:04:15

您可以通过使用 RKObjectLoader 上的 userData 不透明指针来消除配置文件请求之间的歧义,从而解决您的问题。这将允许您在请求上挂起任何您想要的对象,然后可以使用它来帮助区分多个配置文件请求。另外,如果这些配置文件请求将发送到不同的资源路径,那么您可以使用 RKObjectLoader 上的 wasSentToResourcePath: 方法来区分它们。

You can solve your issues with disambiguating between your profile requests by using the userData opaque pointer on RKObjectLoader. That will allow you to hang any object you want on the request, which can then be used to help distinguish between multiple profile requests. Also, if those profile requests are going to different resourcePaths then you can just use the wasSentToResourcePath: method on RKObjectLoader to distinguish between them.

乙白 2024-12-18 02:04:15

我只是在尝试为我自己的 REST 接口找出这个问题时偶然发现了这个问题。我很高兴我这么做了,我现在可能会使用 RestKit。

我离题了,回到你的问题。正如您所指出的,RKObjectManager 中的块参数似乎不应该以这种方式使用。相反,如何编写一个实现 RKObjectLoaderDelegate 的类,接收一个块,并在任何委托调用上调用该块。

也许是这样的?

@interface MyObjectLoaderDelegate : NSObject <RKObjectLoaderDelegate>

@property (nonatomic, copy) void (^callback)(RKObjectLoader *loader, NSDictionary *objectDictionary, NSError *error)

- (id)initWithCallback:(void (^)(RKObjectLoader*, NSDictionary*, NSError*)aCallaback;

@end

在任何实现的委托方法上,您都可以执行该块。由于块保留作用域变量,因此您可以针对调用委托运行代码。

怎么想?

I just stumbled upon this question while trying to figure out this problem for my own REST interface. I'm glad I did, I'll probably use RestKit now.

I digress, back to your question. As you noted it doesn't seem like the block argument in the RKObjectManager is meant to be used this way. Instead, how about writing a class that implements RKObjectLoaderDelegate, takes in a block, and calls that block on any of the delegate calls.

Maybe something like this?

@interface MyObjectLoaderDelegate : NSObject <RKObjectLoaderDelegate>

@property (nonatomic, copy) void (^callback)(RKObjectLoader *loader, NSDictionary *objectDictionary, NSError *error)

- (id)initWithCallback:(void (^)(RKObjectLoader*, NSDictionary*, NSError*)aCallaback;

@end

And on any implemented delegate method you can execute the block. Since blocks retain scoped variables you can run code against the calling delegate.

Whatcha think?

执妄 2024-12-18 02:04:15

我不确定使用块是否是解决您的问题的正确方法。

拥有一个实现 RKObjectLoaderDelegate 的类 GetProfile 怎么样?
因此,您从此处调用请求并将其自身设置为委托。
然后每个请求都有一个 objectLoader。

因此,在您的视图控制器中,每次您执行 GetProfile 时,都会创建一个实例。然后,当该实例向您的控制器发回消息(通过委托?)时,您知道它是什么。

我也正在解决这个问题,所以很想听到反馈。

I am not sure using blocks is the right way to solve your issue.

How about having a class GetProfile that implements RKObjectLoaderDelegate.
So you call the request from within here and set itself to be the delegate.
Then you have an objectLoader per request.

So in your view controller, each time you what GetProfile, you create an instance. And then when that instance messages your controller back (via delegates?) you know which it is.

I am just grappling with this issue as well, so am keen to hear feedback.

情释 2024-12-18 02:04:15

切换到 AFNetworking 似乎是一条出路……对于我所需要的来说,这是一个更容易的实现。

Switching over to AFNetworking seems to be the way to go... it was a much easier implementation for what I needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文