App is crashing if using Core Data - ios

I addded a new attribute (of type boolean) into my core data model and my data.swift class. I try to run my app after I added the attribute. All works fine. Now I try to work with the new attribute. I wrote some code, which work is working with the attribute. Now I tried a 2nd time to run the app. It's crashing. I get the line of code, where the error occurs. But the line of code which I get has nothing to do with the attribute, which I've added new. The line, where the error occurs is working with another attribute in the core data model but not with the attribute which I've added new. Does someone knows a solution for this?
My data.swift class:
import Foundation
import CoreData
#objc(data)
class data: NSManagedObject {
#NSManaged var aufgabe: String
#NSManaged var datum: NSDate
#NSManaged var hatDatum: Bool //new attribute
}
The crashing line:
cell.textLabel!.text = "\(daten[indexPath.row].aufgabe)"
The error message: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) There's nothing written in the console.
My data model: https://www.dropbox.com/s/rzlxi30a5mrez52/Bildschirmfoto%202015-02-01%20um%2014.39.42.png?dl=0

Reset the simulator or delete the app. It's a conflict in you data model files.

Related

SwiftUI preview crash after modify core data entity's attribute

Prior to this issue, the preview is running fine. However, after I have changed the type of the attribute (e.g. from String to Float) inside a .xcdatamodeld file and modify the manual definition file accordingly, which looks similar to this
public class test01: NSManagedObject, Identifiable {
#NSManaged public var var01: String
...
}
The simulation crashes on first few tries but works again after I removed the application inside the simulation iPhone. But then when I tried to use the preview, it always show an error tab saying that it always crashed in updating the view, even the simplest starter file, "Hello World", given when creating the SwiftUI preview file.
Application Specific Information:
Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model." UserInfo={destinationModel=() isEditable 0
At this point I could not find the solution, any idea on this bug/problem?
Try deleting the preview canvas simulators as well. They exist in:
~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/
The preview canvas probably has the old version of the data model.
You may need to do this command at command line:
killall -9 com.apple.CoreSimulator.CoreSimulatorService
And restart XCode as well.

iOS 10 Xcode 8 - migration to Swift 3 and Date

I'm in the process of migration an iOS app from swift 2 to swift 3 and I encounter this error that I don't understand and I'm not sure what to do.
The issue occurs when I try to read the property called dateApproved but not for dateCreated.
print("dateApproved: \(cEntity.dateApproved)")
print("dateCreated: \(cEntity.dateCreated)")
Entity class
#objc(entity)
open class Entity: NSManagedObject {
...
#NSManaged var dateApproved: Date
#NSManaged var dateCreated: Date
...
Upon inspecting the property cEntity I can see that
dateApproved = nil;
dateCreated = "2016-08-24 22:20:38 +0000";
This is a screenshot of the error
Note: it worked before, it just doesn't work anymore since I'm migrated all the code to make it compliant with Swift 3.
Could you please give me some pointers about how to solve/track this issue. Very much appreciated.
The instance variable dateApproved is declared as a non-optional variable, i.e. Swift will assume it's never nil. However, as the debugger shows, it is nil.
Therfore, change the declaration to match reality and make it optional.
#NSManaged var dateApproved: Date?

Xcode 8 Core Data abnormality

I am trying to update my project to Swift 3 which has Core Data. I have confronted with serious error and I really don't know what is going on. My Core Data Model has following properties
#NSManaged var name: String?
#NSManaged var count: NSNumber
#NSManaged var isDelivered: NSNumber
I can set any other properties but isDelivered. When I try to use
myobject.isDelivered = true
I get following error on the console.
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[MyModel setDelivered:]:
unrecognized selector sent to instance 0x6000000d3780'
It looks like Xcode is removing is part from isDelivered property which crashes the app. Is there anything that I can do to prevent this other than updating my model? Thanks.
It is a BUG. It is very confusing bug. For anyone visiting this question, here is the answer I found on Apple forums.
The current version of Swift 3 beta seems to have some flaw about treating properties prefixed with "is".
https://forums.developer.apple.com/thread/50863
Answer from user OOPer
Avoid using "is" in your property name, or if you cannot, try this:
newWriter.setValue(true, forKey: "favorite")
(Update)
Try adding #objc name to the property:
#NSManaged #objc(isFavorite) var isFavorite: Bool

Opening Realm hangs after implementing new object class

I wish to support rearranging a UITableView. I have seen other answers here, here, and here recommend using a another class to manage the realm objects. The only problem is as soon as I add the class I cannot successfully open a Realm.
import RealmSwift
class Data: Object {
dynamic var id = ""
}
// Adding this class causes issues
class DataList: Object {
let list dataList = List<Data>()
}
Any ideas on what is going wrong here? When I attempt to open the Realm it just hangs: no error thrown.
Edit:
From the realm doc it says they should be declared as let.
Thanks to Tj3n for the solution. Migrate your schema or reinstall the app fixes the issue. Here is some doc on that.

Attribute Redeclaration for new NSManagedObject Subclass

This is my first app using Core Data, and I have introduced two new ManagedObjects subclasses. At first I wrote the two classes as two normal Swift classes, then I decided to make them ManagedObject subclasses by creating them as entities in the dataModel.
I then went Editor >> Create NSManagedObject Subclass... and then copied and pasted the code from the first class into the new class, and deleted the old class. This worked fine.
The second time around I tried the same thing, but I am having errors with the attributes this time around.
#objc(TimeValues)
class TimeSlot: NSManagedObject {
var beginDate: NSDate //This line here
var endDate: NSDate //This line here
var currentSeconds: NSNumber //This line here
The attributes all have the error Invalid redeclaration of 'beginDate' and stored property beginDate requires an initial value or should be NSManaged
And in the other TimeSlot extension file I am also getting redeclaration errors:
import Foundation
import CoreData
extension TimeSlot {
#NSManaged var beginDate: NSDate?
#NSManaged var endDate: NSDate?
#NSManaged var currentSeconds: NSNumber?
}
I thought a redeclaration error was because I was declaring it somewhere else, but I have deleted the old folder and the other NSManagedObject subclass works fine. The only difference between the two is that the other one only has static variables.
Any help is appreciated, thanks!
Did you check syntax problems? I had a similar problem caused by an extra empty import statement in files generated by Editor >> Create NSManagedObject Subclass... in Xcode 8.0:
import Foundation
import CoreData
import // <- delete this line

Resources