Wednesday, January 25, 2017

Can we store NSURLRequest directly into Core Data?

Leave a Comment

I am developing iPad Application in which i need to create Multiple NSURLRequest. When is it fails I need to fire that URL request again.

I have three type (Create School, Create Floor and Create Rooms) of request which contain multiple parameters different create Request.

There is retrial method which can fire when there is internet available with same object which was created on first request.

So I was trying to create three Tables and I was trying to store all parameter with their status.

Is it possible to create Single Table with NSURLRequest irrespective of CREATE REQUEST?

3 Answers

Answers 1

You can probably just save the absoluteString of the NSURLRequest’s URL property. Or do they each have distinct timeouts or cache policies?

Answers 2

Core Data entities correspond to instances of NSManagedObject or a subclass of NSManagedObject, so you can't just save URL requests directly. What you could do is create an entity called something like SavedRequest that has a property representing the URL request-- and maybe some other details about the request (whatever other info you might need-- date, maybe?).

Since NSURLRequest conforms to NSCoding, you would create this property using the Core Data "transformable" type. Core Data would use NSCoding to automatically convert to/from NSData as needed. You would assign an NSURLRequest to the property and read them back, and Core Data would save them as NSData.

Given your description though, Core Data might not make sense. It sounds like you just want to save a list of URL requests and later read it back, and don't need the extra features Core Data would provide. It would be simpler to put your NSURLRequest objects in an array and then save that array to a file or to user defaults. You would convert to/from NSData yourself, but since you can use NSCoding that's easy.

To save the array you'd do something like this, assuming an array called myArray containing URL requests and a path in filePath:

BOOL success = [NSKeyedArchiver archiveRootObject:myArray toFile:filePath]; 

You'd get the array back using

NSArray *savedRequests = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 

Answers 3

Yes that is possible, at least with Swift. With Swift you can access the Transformable property in CoreData. Using this property it is possible to put any kind of data in Core Data. That even without extra overhead.

See the excellent tutorial http://geekyviney.blogspot.nl/2015/02/transformable-binary-data-properties-in.html

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment