trying to use coredata with an object class ** unsuccessfully** - iphone

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..

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.

Related

Parse class not created through swift code

I am experiencing an issue when I try to create a new parse class through swift. My code for creating a new class is the following:
let query = PFQuery(className: "newClass")
The issue is that a "newClass" class is not created in my Parse dashboard and I don't get an error message. And since some other code is dependent on retrieving information from this class the app does not function properly.
I have created classes through swift previously and have never run into this problem. I also know that I can create a new class directly through Parse, but that doesn't really help me since there seems to be an issue with the connection between my swift code and Parse.
Thank you in advance for any help!
Is Allow client class creation turned on?
Try using PFObject to create the new class
PFObject(className: "yourNewClass")
I managed to solve it to some extent. After quite some while I got the idea to try to simulate the app on my iPhone instead of only on the simulator that xcode provides. For some reason everything then worked as planned... This is good news for me, but I would like to know why xcode couldn't do it.
Thanks for taking time and giving me some tips Brett!

Is it Possible to put the whole CoreData-Stack in a framework?

I have a very large Project and I am right in a refactoring session. At the beginning my idea sounds good to me, but now i am stuck and confused and near before a git rollback. You guys are my last chance.
We’ve done a GUI application that shows some data that are living in CoreData. A background Process is fetching data from time to time from the network and put them into the CoreData.
So I’ve got the request thing -> pushing into CoreData and <- getting it out onto the GUI.
That works for very good for us. It’s straight forward and nothing special. My Company is programming this application since January and the requesting and data part gets huge. We also have a 100% test coverage! \o/. Working in that project could be so nice… if… if the GUI things could be separated.
As time goes by I have to split the dev-team into two parts: one for the gui (and the several gui applications that all based on the same data model) The second one for the Request-and-CoreDate part. My Model interface is clean enough to have a nice entry point, so i thought i would be easy to move the Request and CoreData sections into a library-project in my Workspace.
hm, well. no!
I put the Kit.xcdatamodeld into the new library project. I generate all models and add all the code from the main application into the lib. I resolved every problem and start to move the first basic tests into the new project.
I put the managedObjectModel part from our ApplicationDelegate into a DataController in the new project. And changed the first call to use this managedObject from the new class.
let dc = DataController()
_ = GlobalScope(context: dc.managedObjectContext!)
When I run the first test that should store a simple entity than an error occurs in managedObjectModel:
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource(" <LibName>Kit", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
fatal error: unexpectedly found nil while unwrapping an Optional value
My Question is: Is it possible to do the whole CoreData stuff into a Framework? I wan’t have any data model or requests in my gui applications? The plan ist to include the framework and use the getXY() setXY() functions of my models. Also the whole network background fetching should be places in the framework.
But it seams that there is no mainBundle in a framework. So how to get the database the right way?
Thank you very much.
ps
First, Core Data is already a framework. You are looking to put your layer on top of Core Data into a framework. Is it possible? Probably, but it is not a great idea.
There is no value in putting a third (or more) of your application into a framework. It will not make anything cleaner or easier. In fact it will make debugging of your application harder.
Separate out the code using groups in Xcode and call it there. Trying to put things into separate Xcode projects (which is what you would need) would lead to more issues than it is worth.
BTW, there is no way to compile the model into the framework as that is an independent file. Therefore your primary application would need to embed the model file anyway.
You could explore creating your own custom CocoaPod like structure but again, it really is not worth it and maintainability will decrease.
The problem is this line:
let modelURL = NSBundle.mainBundle().URLForResource(" <LibName>Kit", withExtension: "momd")!
It's looking in the main bundle for that resource, which doesn't exist. You'll want to use bundleWithIdentifier or bundleForClass instead, like:
let modelURL = NSBundle(identifier:"com.whatever.yourFrameworkIdentifier").URLForResource...

MVC Default Edit View increments the id property of my model? Can't figure out why. .

I'm trying to learn web development, and I bet that this is a simple problem and that I'm overlooking something obvious.
In my default crud edit controller (generated using the MVC framework) I retrieve my model from a localDB instance using my EntityFramework's dbContext. That is sent to the View. In the debugger just before the controller call to return View(model) I can see that the Id is set to 2.
When the [post] edit controller is fired I see that the identity property is 3. I wanted to figure out why so I changed the View to display my Id property and I see that it is 3 as soon as I render the page. Last time I saw it the property was 2, now it is 3.
I don't know how to hook into any logic that would happen between the time I send off my model and when when the view is rendered.
Can anyone help me learn how to debug this so that I can figure out why my Id property is incremented when I pass the model into the view?
I don't know how EntityFramework works but I have worked with CakePHP. My advice is like this:
Usually PHP frameworks have debug mode which you can set in the configuration file (turn it on or off). Usually stack of operation executed is also displayed in debug mode or there is a simple way to do that. There's also for example in CakePHP exist function debug($yourVariable); try to search for sth. similar
It seams like not the edit happens but new row is inserted to the database. Check your database for this. I recommend to debug the id of the column being passed for edit action and check if there's the same id in the database first of all.
Hope something helps.
I figured it out. Posting here for anyone else that comes across my problem.
I tried dumping the whole model out without using any of the htmlhelper methods. (In my case I am uinging: Html.HiddenFor helper) When I did this I saw that the value in the model was what I expected it to be. So I began investigating why the helper methods might be broken. Google worked for me here :)
Turns out, when the helper methods run they first check the ModelState dictionary for the desired value. In my case I was looking for a value that was in my model object as well as the ModelState dictionary, because the name was very common: Id.
To fix the issue before I call return View(model) I call ModelState.Clear() in order to make sure there are no values in the dictionary that are in conflict. Doing this causes my page to be rendered correctly.

UIManagedDocument example / tutorial

I have been trying very unsuccessfully to create a simple UIManagedDocument library style application (separate documents saved to iCloud).
I am confused with the following:
Do I subclass UIManagedDocument and set up the persistentStoreCoordinator, ManagedObjectModel & ManagedObjectContext within this subclass, or are these supposed to be configured within the AppDelegate (and if so, how do I go about refreshing the persistentStoreCoordinator to look at the new file - it seems that once that has read a persistentStore that I can't get it to read a new persistent store)?
Richard's example is an excellent example. I used it and the PragmaProg book on core data http://pragprog.com/book/mzcd2/core-data as a guide for creating my managed document module on github.
See: https://github.com/dtrotzjr/APManagedDocument
My code makes use of iOS 7 iCloud Core Data behavior which I cannot comment on publicly until the NDA is lifted. Feel free to ping me directly if you have any questions.
I've just posted a project based on Rick Warren's example: MultiDocumentPlusUUID. In his "Syncing Multiple Core Data Documents Using iCloud" post, Rick writes:
Another bug often shows up when I start trying to pass changes back and forth between devices that both have the same file open. The first sync always seems to work--and it seems to work pretty well as long as each subsequent sync is in the same direction.
MultiDocumentPlusUUID compiles and runs on iOS 7.1b4, and can ping-pong updates to a given document successfully.

iphone coredata with two xcdatamodel

I've been working with core data (successfully ;) , following some tutorials and kind of getting all, but now I have a question about some basic tutorial I found some time ago (the author is not responding so here we go!)
this is the tutorial, and the code for it is here
in this tutorial, there are 2 xcdatamodels??, why? what is the timeStamp on the Event does?? I checked it in sqlitemanager, and after creating some data, timestamp doesnt save anything,
so was it a mistake to leave the Event entity? with its datamodel?
or is there something Im missing?
thank you!
It looks like CoreDataTutorialPart1.xcdatamodel (the model file containing the Event entity) was auto-generated by the Core Data Xcode template and isn't actually used anywhere in the project.