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.
Related
I just declared an entity called "Users" array:
var UsersArray = [Users]()
I got this error:
use of unresolved identifiers "Users"
hint : I did import CoreData and created the entity
Please try this solutions !!
Solution 1 :-
Product->Build for testing solved this issue for me(it solves most unresolved identifier bugs for some strange reason)
Solution 2 :-
changing import Foundation to import UIKit.
It seems like UIApplication isn't included in Foundation framework.
Solution 3 :- Highlight the Data Model, go to Editor -> Create NSManagedObject Subclass...
Solution 4 :-
Create the NSManagedObject files and then delete them and everything is fixed.
Hope this helps thank you.
If your entity is actually created, just close and re-open XCode. This works for me everytime. The other solutions, such as clean build, build etc, didn't work for me.
Just ran into this issue after renaming an entity and figured I should post here how I resolved it.
Typically after making changes in the data model and getting compilation errors, I just do Product > Clean Build Folder, then Product > Build and that should take care of the errors.
However, the steps above didn't help this time. After scratching my head for while, I found the culprit with following steps:
In the Project navigator, select your .xcdatamodelId file
In the Configurations section, select the appropriate configuration
Look for the entity in question and make sure the class name is correct (in my case, the entity and the class were not the same)
Other possible solutions:
(A) Re-launch Xcode
This is the solution that worked for me. (See this answer)
(B) Check if you did not forget to finalize your entity description
Typically: forgot to assign a type to one of the attributes (still "undefined").
(C) Clear derived data
Reason: The generated core data managed objects subclasses's source code files are in the project’s derived data location.
Note: you might have to re-link your swift packages is using SPM.
I also had to deal with this issue even working with the latest Xcode12.3 on a SwiftUI project where I didn't checked the CoreData option at project creation.
After adding a class to instantiate the NSPersistentContainer, loading the stores, creating model.xcdatamodel file with an entity and a text field plus a button on a view, then it shows an error saying: "cannot find Item in scope".
It's a bit difficult to write code when you expect the entity to help out with autocomplete. First, I thought I forgot to initialize my class or missed a bracket, but it wasn't that either.
I tried cleaning up, rebuilding and running the project several times with build failed as result. I had to close this particular project then all errors got dismissed. No needed to restart Xcode.
I faced the same issue. Doesn't included Coredata on the beginning of the project. Later added necessary files and core data stack. But when trying to fetch the data from the Entity name and the Xcode throwing the same error.
Later, I closed the project and again opened on Xcode and it started to work.
I had this issue with an .xcdatamodel that I had created in another project and moved it into this one.
I solved it by selecting the .xcdatamodel file, turned of the "File Inspector" and then under "Target Membership", I made sure that all the targets were selected.
for me the entity name and class name was different, and I was refering to entity name for Data Type
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.
I am creating an application using Core Data. But When I run my app it crashes. It shows me this reason:
reason = "The model used to open the store is incompatible with the one used to create the store";
What should I do now?
I think you need to refer this link and refer to answer given by Grouchal in the link below:
What do I have to do to get Core Data to automatically migrate models?
If you need any further help please do let me know.
Hope this helps you.
It means that you created a model, run the app, modified the model (by adding or removing managed objects, renaming object properties and so on...) and reopened the app without migrating the data first. I still have to try this by myself but, you should definitely take a look here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Introduction/Introduction.html
You can just delete the app (icon) from the simulator or device. Then, rebuild and run.
As dave explained, you have modified the core data structure (the store). If you're using the standard template, in the new rebuild a new model (NSManagedObjectModel) will be created that is compatible with your newly modified store.
You will probably be making many changes to the core data structure (adding or deleting an entity or some property or relationship), and, each time you do, you can just delete the app from the simulator or device.
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.
I am just learning Core Data on iPhone and the tutorial I am following says to create a new file and select NSManagedObject as the template. However, in Xcode 3.2.2 it seems to be gone. Is there any way to get it back or some kind of workaround?
Thanks in advance!
Select the entity in your Core Data model (xcdatamodel) file first, and then create a new file. The managed object template should show up.