创建“呼叫” table : 序列化一个Block还是NSOperation?
当我的应用程序离线时,我想存储一些对服务器的调用,以便稍后当应用程序重新上线时重播它们。
我想知道是否可以序列化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSOperation
不符合NSCoding
协议;所以我认为对于归档它的实例来说答案是“不”。我相信,对于块的答案相同。也就是说,您能否将实现此策略所需的数据包装到继承自
NSObject
的类中,然后将其序列化?换句话说,不要尝试序列化代码;相反,序列化代码所需的数据。不过,在不了解上下文的情况下很难知道这是否是一个可行的解决方案。NSOperation
doesn't conform to theNSCoding
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.