Vincent Bacalso
Vincent Bacalso

Reputation: 2147

Save a POST Request to Core Data when there is No Internet Connection

I'm trying to develop an iOS App that saves an ASIFormDataRequest (i.e Post with Images, Comments, etc.) like how Twitter App saves a tweet to its Drafts, or Path App and Instagram as well..

will it be possible to just save the "ASIFormDataRequest *request" instance directly to Core Data (which is to a type I don't know)

or should I create different entities depending on the form request i want and save the request's data? Like for example,

[request setPostValue:@"something"    forKey:@"method"];
[request addPostValue:object1         forKey:@"key1"];
[request addPostValue:object2         forKey:@"key2"];
[request addPostValue:anImageURLPath  forKey:@"key3"];

..and Create an entity for the above request with attributes of the request's post values?

Upvotes: 0

Views: 454

Answers (1)

Robotnik
Robotnik

Reputation: 3800

Probably best that you only save the values that you want to keep. You should create entities to store your values.

You could have one entity for any form values, with a one-to-many relationship to another entity- which is a simple key/value pair for the post values that you have

this tutorial is the one I used to learn basic CoreData. It walks you through creating entities and making relationships between them

EDIT: In answer to your question in the comments:

I would assume that you have models (data classes/entities or whatever you call them) to represent 'A Post'. I haven't seen your code and I'm not sure how you've written it, but this is how I would implement it.

1) Have a form that gets data from the user. (and a model representing this form)

2) The User hits 'Send' which passes that data (as a model) to a Network Service, which converts it into a FormDataRequest and tries to send.

3) If The Network fails, (or if the user just hits 'Save Draft'), the model is then sent to a Data Service which Saves the model to CoreData. (creates a CoreData Entity -which should be similar if not the same as the model- copies the values over, and saves the entity)

as (half) pseudocode :P

class MyFormViewController{
    // Obviously declared in the header file ;)
    TextField name;
    TextField dob;
    ...
    // View Stuff dealing with displaying the form
    ...

    function getModelForFieldValues() {
        Model m;
        m.name = name.Text;
        m.dob = dob.Text;
        return m;
    }

    function send_buttonPress() {
        Model myModel = getModelForFieldValues();
        BOOL success = NetworkService.send(myModel); //Attempts to send the data over the network
        if(!success) {
            DataService.save(myModel); //Saves the model data to CoreData
        }
    }

    function saveDraft_buttonPress() {
        Model myModel = getModelForFieldValues();
        DataService.save(myModel);
    }
}

Upvotes: 1

Related Questions