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.
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.
精彩评论