0

I am completely lost trying to set up core data to be used inside my nsobject class. I am adding coredata to my existing project so its quite hard to figure this all out. Because of this I have started a new project with coredata to get the sample code I need to implement coredata in my current app.. however I am wondering what alterations do I need to make in order to use this coredata sample code with an object class instead of a view controller/tableview which is what the template code is doing?

To help answer my question I will explain what I am doing with my project at the moment.

I have several viewcontrollers that display different sets of data, because I have a custom database engine I am having to communicate with I have two classes that I have made. One is a request class which creates a packet with all sorts of data that gets sent off to the DBEngine using NSURLRequest/NSURLConnection.

I am using NSURLRequest/NSURLConnection delegates in my custom request class, so when send my request off to the DBEngine i wait until I get a response inside connectionDidFinishLoading delegate method, which I then pass the response data over to my responses class like so....

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // depending on what sorta request is made will depend on how the data shuld be handled.
    if ([methodName isEqualToString:@"GetDBVersion"]) {
        //tbc
    }
    else if ([methodName isEqualToString:@"GetManuf"]) 
    { 
        [engineResponses GetManuf:receivedData];
    }
    else if ([methodName isEqualToString:@"GetNonM"]) 
    {
        [engineResponses GetNonM:receivedData RestrictionID:RestrictionID];
    }        
}

By doing this I am creating a new instance of my engineResponses class, which Is okay untill I try to pass the context from the application delegate over to this same class thus creating another instance which equals to things not working....

So as stated above I am wondering how I can edit the template code to work in my favor... I hope I have been clear in my explination I have just spent the last hour on trying to get this question perfect as I have spent the last two days chasing my tail trying to figure this stuff out.. its the first time I have worked with coredata and im just finding it hard to come to grips with it because I seem to be using it in an unconventional manner....

any help would be HUGELY appreciated.. if you need any more code example or better explanations please ask.. I will do anything I can to get help..

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • Core Data is used for local application storage. However you seem to be describing a remote database (NSURLConnection, etc). Could you elaborate on what you are using core data for? All Core Data objects by default inherit from NSManagedObject, which is an object. Have you seen this page: http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started ? – borrrden Apr 30 '12 at 02:34
  • I am using coredata to store (a list of values that are in NSData format) NSData that is being returned from the server as per my request. I will then in the future use core data like a cache untill my db version changes and I have to update the data in my coredata object. I understand there are better solution for this that apple provide in the form of NSURLConnection delegates (caching) etc, however due to the custom headers I am having to use as per outlined for the DBEngine I am communicating with the need for a more unconventional caching laying is needed to communicate with the dbengine – C.Johns Apr 30 '12 at 02:47
  • I think I have possibly solved my problem.. however need help figuring out if there is anything in my coredata.. basically I have set up a singleton design pattern for accessing my engineResponses.. so I am pretty sure that I was initializing two instances of this object class.. once from my other object class and another from the appdelegate... now I need to find a way of testing if the data is actually in the coredata object now.. but im not totally sure on how to do this. – C.Johns Apr 30 '12 at 02:52

1 Answers1

0

Okay So I found out the problem was with declaring multiple instances of the same object.. So all I had to do was use a singleton design patter to get it working perfectly.

Alot of people dislike singletons but I am not sure how else to get around this issue???? any suggestions would be greatly appreciated.

Also I know this worked because I have since checked the DB by logging the storeURL in the appdelegate, then copying pasting that dir, into the goto file - then opening up the sqlite db in xcode.. all the values are in there perfectly.

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • Singleton patterns definitely have their uses – danielbeard Apr 30 '12 at 03:13
  • lol, yep.. I think so also. When I was at uni a couple of my programing teachers spoke about them alot and how useful they are etc. However that was for other languages not objective C, when ever I read anything on stackoverflow about singletons and ObjC there are lots of haters. lol I just want to make sure I'm not doing things incorrectly. – C.Johns Apr 30 '12 at 03:19