views:

135

answers:

1

Hello everybody. can nybody give me an example which will explain the process of storing the data in coredata which is received from webservice. i dont want to use sqlite DB. perhaps a link which demonstrates this kind of example.

+1  A: 

There really isn't any particular trick related to web services in particular. Once you get the data from the service, you create instances of your Core Data classes and then populate them using the data just as you would with data from any other source such as the UI.

For example, most JSON implementations provide the returned data is a dictionary. You just set the properties in the core data object to the appropriate values in the dictionary.

If you're not familiar with how to setup and use core data, you should learn that first.

Edit:

From comment:

oh ok. so i understand how to assign a location of the file if it is in documents folder. but in this case i am getting the data from a webservice which is received in the form of a response. so wat do i do in this case? there will be no location to assign the store

There is no connection between the source of the data and location of the store file. In the normal case, you won't be connecting to a Core Data managed store on the webserver (possible but rare.) Instead the data will be coming from the server is a standard format such as JSON, REST, etc. Your app will create the local store (wherever you decide to put it) and initialize the Core Data stack. Then it will take the output of the JSON parser and create NSManagedObjects to represent the data. Then it will save those objects to the store just as it would with data form any other source.

So, in pseudocode it would look something like:

Core Data{
    Create local persistent store in desired location;
    Create managed object context;
    Load managed object model:
} 

Network Inteface {
    Connect to Server;
    Send request;
    Parse request into objective-C data structure (array, dictionary, etc)
}   

Load Data strucutre into Core Data{
    Either {
        Insert into context NSManageObject for entity that models recieved data
        or
        Insert into context instance of NSManagedObject subclass that models the recieved data
    }
    Set attributes of entity/instance to the appriopiate fields in the recieved data structure. 
    Save managed object context (which saves the entity/instance to the file on disk)   
}

I think you're thinking this is a far more complex and involved process than it is. It is basically no different than saving data from a server to a local text file. The only difference is that you are saving to a Core Data stack instead.

TechZen
yeah i understand the process but it will be really helpful if there is an sample example. can u plz provide me with an example of core data with webservices?????
Jayshree
i want to know, where will the NSPersistentStoreCoordinator look for the data? if there is no SQLite DB, where should i ask it to search? bcoz i will be getting the data from webservice, and will be entering it into datamodel class. can u give me example code for this.
Jayshree
I don't have a sample of exactly what you're doing. However, its not necessary. When you create the persistent store, you assign a location for the actual file of the store. The preferred location is the application's library folder followed by the documents folder. Look at the app delegate of one of the Xcode template projects. The persistentStoreCoordinator accessor method shows how to set the location of the store file. You can also assign an off device URL for the store but this is seldom done. I think you believe this to be more complicated than it is. The store is just a file.
TechZen
oh ok. so i understand how to assign a location of the file if it is in documents folder. but in this case i am getting the data from a webservice which is received in the form of a response. so wat do i do in this case? there will be no location to assign the store.
Jayshree
See my edit for details.
TechZen
hey thnx so much. this really cleared some of my misconceptions. thnx for the help
Jayshree