Can we store NSURLRequest directly in Core Data?

I am developing an iPad application in which I need to create several NSURLRequest. When this fails, I need to run this URL request again.

I have three types (Create School, Create Floor and Create Rooms) of the request, which contain several parameters that distinguish the create request.

There is a repeated request method that can be triggered when there is access to the Internet with the same object that was created on the first request.

So, I tried to create three tables, and I tried to save all the parameters with their status.

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

+5
source share
4 answers

Core Data objects correspond to instances of NSManagedObject or a subclass of NSManagedObject , so you cannot just save URL requests directly. What you can do is create an object called SavedRequest that has a property that represents the request for the URL, and maybe some other details about the request (what other information do you need - date, maybe?).

Since NSURLRequest conforms to NSCoding , you must create this property using the Transformable Core Data type. Core Data will use NSCoding to automatically convert to / from NSData as needed. You assigned the NSURLRequest property to the property and read them back, and Core Data would save them as NSData .

Given your description, Core Data may not make sense. It looks like you just want to save a list of URL requests and then read it and don't need the extra features provided by Core Data. It would be easier to place NSURLRequest objects in an array, and then save that array to a file or by default for the user. You convert to / from NSData yourself, but since you can easily use NSCoding .

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

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

You will get the array back using

 NSArray *savedRequests = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 
+1
source

So, I came up with my own idea, which I share with everyone.

According to the read and googling, I cannot store NSURLRequest in the master data. because I do not know the type of object.

I will create a basic data table with columns PARAMS, METHODS, URL

So, I can create a table that will contain a PARAMS (Dict) column, methods (PUT, GET, etc.), URL.

What will I do to convert the DICT parameters to NSKeyedArchiver and save it in the PARAM column. URL and METHODS in a separate column.

When I need a save request, I can grab it from the table and send it to the server.

+2
source

Perhaps you just save the absoluteString NSURLRequests URL property. Or do each of them have separate timeouts or caching policies?

0
source

Yes, this is possible, at least with Swift. With Swift, you can access the Transformable property in CoreData. Using this property, you can put any data into Core Data. This is even without additional overhead.

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

0
source

Source: https://habr.com/ru/post/1262687/


All Articles