last day (15 days later) » 

7:48 PM
0
Q: Items are not added to Core Data when saving context

KobeI am trying to build a Core Data app and I am using some code based on this book. To simplify code I have something like this: class City: NSManagedObject { @NSManaged public private(set) var name: String @NSManaged public private(set) var population: Int public static func insert(...

 
How did you inspect the sqlite file? Have you tried performing a fetch to confirm that the db is empty?
 
@SeanCAtkinson: Yes its an MainQueueConcurrencyType and it also attached to a persistent store coordinator. I am creating it in my main view controller and passing it along to the other view controllers.
@pbasdf: I checked with an sql reader software. But I am trying to fetch them when I first run the app.
 
Does your sql reader use the WAL and SHM files, as well as the .sqlite?
 
No, I just load the .sqlite file. But still, I am fetching the data when I run the app, and I got nothing (count is 0)
@SeanCAtkinson: added. Let me know if more code is needed.
 
Try enabling SQLDebug to see whether the inserts are happening.
 
7:48 PM
@pbasdf: I have done this and I edited the question with the output. I am not sure what the output is saying. Is it correct ? Wrong ?
 
It shows you are inserting into a table called ZTRANSACTION - which suggests you have a separate Transaction entity which you insert?
But no sign of an insert into the City entity.
 
I just used City as an example here because the code is simplified, but in my real project is called Transaction.
 
In that case, it looks like the save is working correctly. Double check the code where you fetch.
 
for now I am just trying to do countForFetchRequest, to at least see the count, but its always 0
I am double checking the code now.
but I don't see any issues
2016-06-18 22:52:18.087 Savings+[1203:50984] CoreData: annotation: Connecting to sqlite database file at "/Users/astanciu/Library/Developer/CoreSimulator/Devices/2451D7B7-3814-4C83-AAE5-E4CE53306A4E/data/Containers/Data/Application/60D0D817-0FC0-4BFE-8736-0CD02697E01B/Documents/Transaction.sqlite"
2016-06-18 22:52:18.088 Savings+[1203:50984] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
2016-06-18 22:52:18.090 Savings+[1203:50984] CoreData: sql: pragma journal_mode=wal
this is from when I start the app
2016-06-18 22:52:18.097 Savings+[1203:50984] CoreData: sql: SELECT COUNT( DISTINCT t0.Z_PK) FROM ZTRANSACTION t0 WHERE t0.ZNAME IS NULL
I found this strange, ZNAME should be NULL ? :/
or this means that it should retrieve all items...I am not sure
 
No, it shouldn't be adding the WHERE clause. Have you specified a predicate?
The final three lines of the SQLDebug output show that the fetch without the WHERE clause returns 3 items.
 
7:57 PM
yes, this is my predicate:
public static var defaultPredicate: NSPredicate {
    return NSPredicate(format: "%K == NULL", "name")
}
ohh, ok, so I have data there, this is good
ohh....after removing the predicate, I can see the data
:)
 
Thats good news
 
so specifying the defaultPredicate is my problem, cause its wrong. You can provide this as an asnwer to my question and I will accepted :)
 
Sure, thanks.
 

last day (15 days later) »