创建“呼叫” table : 序列化一个Block还是NSOperation?

发布于 2024-12-09 01:40:17 字数 529 浏览 0 评论 0原文

当我的应用程序离线时,我想存储一些对服务器的调用,以便稍后当应用程序重新上线时重播它们。

我想知道是否可以序列化 Blocks 或 NSOperations 来保存它们(例如使用核心数据)?

实际上,这似乎并不简单:

id block = [^{
        int i =0;
    } copy];
NSData *myEncodedObjectToSave = [NSKeyedArchiver archivedDataWithRootObject:block];

这会引发一个无法识别的选择器:

-[__NSGlobalBlock__ encodeWithCoder:]: unrecognized selector

您知道我如何实现这样的“调用”表吗?

编辑

我正在寻找类似rails中的delayed_job之类的东西,用NSOperation或块创建一个表,我可以在将来的某个时候执行。

When my app is offline I would like to store some call to the server in order to replay them later when the app goes back online.

I'm wondering if it would be possible to serialize Blocks or NSOperations to save them (with core data for example)?

Actually it seems that it is not straightforward:

id block = [^{
        int i =0;
    } copy];
NSData *myEncodedObjectToSave = [NSKeyedArchiver archivedDataWithRootObject:block];

This raises a unrecognized selector:

-[__NSGlobalBlock__ encodeWithCoder:]: unrecognized selector

Do you have any idea how I could implement such a "call" table?

Edit :

What I'm looking for would be something like delayed_job in rails, creating a table with NSOperation, or blocks, that I can execute sometime in the future.

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

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

发布评论

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

评论(1

今天小雨转甜 2024-12-16 01:40:17

NSOperation 不符合 NSCoding 协议;所以我认为对于归档它的实例来说答案是“不”。我相信,对于块的答案相同。

也就是说,您能否将实现此策略所需的数据包装到继承自 NSObject 的类中,然后将其序列化?换句话说,不要尝试序列化代码;相反,序列化代码所需的数据。不过,在不了解上下文的情况下很难知道这是否是一个可行的解决方案。

NSOperation doesn't conform to the NSCoding protocol; so I think the answer is 'no' to archiving instances of it. Same answer for blocks, I believe.

That said, could you wrap the data needed to implement this strategy into a class that inherits from NSObject then serialize that? In other words, don't try to serialize the code; rather, serialize the data that the code requires. Hard to know whether this is a viable solution without knowing the context, though.

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