Core Data deleteObject sets all properties to nil - ios

I'm a little new CoreData.
When I call deleteObject() with object on my NSManagedContext object, its setting all the properties in the object to nil. Is there anyway for me to avoid this? I don't want to be nullified.
My project is in Swift.

You're misunderstanding the purpose of CoreData. It's a way of managing a persistent store, which means that whatever you tell your context, is absolute. So if you deleteObject(), that object gets prepared for deletion and you're not supposed to touch it anymore.
Instead, you want some kind of mirror object, that allows you to create a new copy of the NSManagedObject for in-memory use. You could do it like this;
struct MirrorManaged {
var text: NSString
}
class Managed: NSManagedObject {
#NSManaged var text: NSString
func copyToMemory() -> MirrorManaged {
return MirrorManaged(text: self.text)
}
}

Related

KVO not working for custom property of NSManagedObject

I have a subclass of NSManagedObject Folder with a state of Availability
#objc enum Availability: Int16 {
case unknown
case available
case unavailable
}
Folder has to do extra stuff (like delete related files) whenever it's availability changes. So I have
internalAvailability saved in core data
Computed property availability using above property
`
extension Folder {
#NSManaged private var internalAvailability: Availability
}
extension Folder {
private func deleteFiles(...) {
...
}
#objc dynamic public var availability: Availability {
get {
return internalAvailability
}
set {
willChangeValue(forKey: "availability")
deleteFiles()
internalAvailability = newValue
didChangeValue(forKey: "availability")
}
}
}
Using Reactive, I want to change navigation item's title based on availability but the signal is never called after once!
```
let property = DynamicProperty<NSNumber>(object: folder, keyPath: "availability")
internalVariable = property // To have a reference of property
navigationItem.reactive.title <~ property.map { (stateNumber) -> String in
guard let a = Availability(rawValue: stateNumber.int16Value) else {
assertionFailure()
return ""
}
let prefix = a == .available ? "" : "(Nope) "
return "\(prefix)\(folder.name)"
}
I have explicitly added KVO compliance to the property in hopes that this starts working, but alas no results.
Edit: if I create the DynamicProperty on internalAvailability instead of availability, everything works smoothly..
Adding as an answer since it became a learning exercise. Hopefully someone else too would be benefitted.
The app uses a multiple managedObjectContext(moc) architecture. 1 private moc to make changes and 1 main thread moc that synchronises itself using mergeChanges.
In above code, navigationItem is using the folder instance kept with main-moc. The DynamicProperty is listening to KVO changes on this main-moc's folder instance. Let's call this main-folder. When I make changes, I modify the folder instance we have on private-moc. Let's call it private-folder.
On modifying private-folder and calling save on private-moc, a notification of name NSManagedObjectContextDidSave is broadcasted. main-moc synchronizes itself using mergeChanges.
mergeChanges changes main-folder, but notice that it would never call the computed-property-setter availability. It directly changes internalAvailability.
And thus, no KVO notifications are posted of our computed property.
TL;DR When doing KVO on a NSManagedObject subclass, use a stored property instead of computed one. In case you have a multi-moc (managed object context) scenario and use mergeChanges to synchronise, setter for your computed property is not called when synchronising.
Edit (Solution): add method of the pattern keyPathsForValuesAffecting<KeyName> KVO relevant documentation
#objc class func keyPathsForValuesAffectingAvailability() -> Set<NSObject> {
return [#keyPath(Folder.internalAvailability) as NSObject]
}
When using Core Data we use the NSManagedObjectContextObjectsDidChange notification instead of KVO. This brings many advantages including coalescing of change events and undo support. If we need to know what attributes changed on an object we can examine changedValuesForCurrentEvent which even includes transient attributes that have a matching keyPathsForValuesAffecting.... These advantages likely outweigh those from a KVO binding framework aka reactive.

ios swift models structure with realm database

I am developing my iOs App and I am using Realm database. As I am totally new to ios developing (also swift and xcode) I have question about structuring data (I've already read some general project structure guidelines but couldn't find the answer). My thinking is connected with Java structures
For Realm databases (RealmSiwft) I created a model like this:
#objcMembers class Patient: Object {
dynamic var patientId:Int = 0
dynamic var refNumber:String = ""
convenience init(id:Int, refNumber:String){
self.init()
self.patinetID = id
self.refNumber = refNumber
}
}
Now, it looks just like a POJO class in Java. But as I learned, this model structure is made that way so it can be able to use Realm.
So the question is, if I need somewhere else in my project to use Patient objects, is this Realm-POJO-model good to use? I mean, should I use it just like a normal Model even when I dont need to make database operations on it? Or should I make this Realm model alike DAO class for databases operations and make another model class like Patient.swift for whenever I want to play with Patient without using databases (I hope not, cause it's so much code duplicating)
And what if I need variables in that Patient Model that won't be stored in database? Can I make it without dynamic? What about init than? That blows my mind, as far as I learn swift it seems so ugly and unstructured, or I just can't switch to it yet...
if I need somewhere else in my project to use Patient objects, is this
Realm-POJO-model good to use?
even when I dont need to make database operations on it?
You can use your Patient object without savings to the DB, move them to different controllers and so on.
what if I need variables in that Patient Model that won't be stored
in database?
Look to ignoredProperties() method.
Can I make it without dynamic?
No you can't because of Realm based on Objective-C object, so this is necessary type.
What about init than?
You can create different Constructors methods, look to the Initialization doc. In case with Realm you should setup values to noticed variables (if you don't give them Default Property Values)
Your class should look like this:
class Patient: Object {
// MARK: - Properties
#objc dynamic var patientId: Int = 0
#objc dynamic var refNumber: String = ""
// MARK: - Meta
// to set the model’s primary key
override class func primaryKey() -> String? {
return "patientId"
}
//Ignoring properties
override static func ignoredProperties() -> [String] {
return ["tmpID"]
}
//It's ok
convenience init(id:Int, refNumber:String){
self.init()
self.patientId = id
self.refNumber = refNumber
}
}
All other detail information you can find in: realm docs
Also you can extend you base code with swift extension:
extension Patient {
var info: String {
return "\(patientId) " + refNumber
}
func isAvailableRefNumber() -> Bool {
return refNumber.length > 6
}
}

Save EVObjects with CoreData

I need to save some data with CoreData. Generally thats not a problem at all. The problem is, that the data is created with EVReflection an therefore inherits the class EVObject. To save the gathered data to CoreData they have to also inherit NSManagedObject. The problem is that swift does not allow you to inherit multiple classes. Would appreciate any tips.
class Device : EVObject
{
var channel : [Channel] = [Channel]()
var name : String = ""
var ise_id : Int = 0
var unreach : Bool = false
var sticky_unreach : Bool = false
var config_pending : Bool = false
override internal func propertyMapping() -> [(String?, String?)] {
return [("name", "_name"), ("ise_id", "_ise_id"), ("unreach", "_unreach"), ("sticky_unreach", "_sticky_unreach"), ("config_pending", "_config_pending")]
}
}
You don't have to inherit. You can extend them. Example:
class User : NSManagedObject{
#NSManaged .....
}
//Extension
import EVReflection
extension User : EVReflectable { }
https://github.com/evermeer/EVReflection#extending-existing-objects
Note I'm not aware of EVReflection but I think this answer can generally apply.
Don't use multiple inheritance. Have two separate classes and a mechanism for creating/loading/updating one object from the other. Protocols may allow it to be done in a way that minimises translation boilerplate (possibly using valueForKey(_:) and setValue(_:forKey) if you can know the key names in a safe manner.
It may not even be even be necessary to have an NSManagedObject subclass but just have an instance of NSManagedObject in all your classes that is loaded/created/saved as necessary.
It depends on what functionality you want to use from EVReflection. Since NSManagedObject also has NSObject as it's base class you could use most functions by just setting NSManagedObject as your base class instead of EVObject.
The only thing you have to do is instead of calling EVObject functions directly, you have to implement the code snippets that are in that EVObject method. Almost any function there is just a convenience method that will call the corresponding EVReflection function.
If you have any questions in the future, then please also report this as an issue on GitHub.

Mac OSX - Core data isn't stored

In my app I use two table views that are bound to each their NSArrayController and the array controllers are set to use Core Data entities. When data is generated, an NSObject is created and values are stored in it with obj.setValue(_:forKey:). After this the object is simply added to the array controller with ac.addObject().
Shouldn't this suffice to have Core Data taking care of persistent storage of the data?
In any case, if I try to save the data by calling saveAction() it tells me that the MOC has no changes (moc.hasChanges = false) so it doesn't even begin to save the data with this method.
What else do I need to take care of to make Core Data store the data properly and acknowledge changes?
The array controllers are set in Interface Builder as follows:
Mode: Entity Name
Entity Name: 'name of entity in data model'
Prepares Content is checked
They are also correctly bound to the managed object context.
Simplified, relevant code from my app:
/* Clear existing data. */
let range:NSRange = NSMakeRange(0, arrayController.arrangedObjects.count);
let indexSet:NSIndexSet = NSIndexSet(indexesInRange: range);
arrayController.removeObjectsAtArrangedObjectIndexes(indexSet);
let array = generateData();
/* Generate data. */
for i in 0 ..< array.count
{
let data = array[i];
/* Create new data object. */
var obj:NSObject = arrayController.newObject() as! NSObject;
obj.setValue(data.name, forKey: "name");
obj.setValue(data.type, forKey: "type");
obj.setValue(data.category, forKey: "category");
/* Add it to the array controller's contentArray. */
arrayController.addObject(obj);
}
UPDATE:
It looks like my app is instantiating four MOCs when it launches. I suspect that the way how I add them in the Storyboard for the two array controllers is wrong. I added an NSObject to the two table view controllers (which also contain their array controllers) and set their base classes to be my CoreDataDelegate (which is my class for the core data code that is normally in AppDelegate). I suspect this is where the multiple instances of CoreDataDelegate are created. The question is: How should I do this right so that the array controllers can reach my CoreDataDelegate class?
I made my core data delegate class a Singleton which solved the problem for me...
class CoreDataDelegate : NSObject
{
static let instance = CoreDataDelegate();
...
}
Then I have a reference to it in my AppDelegate (which is also a singleton) ...
#NSApplicationMain
class AppDelegate : NSObject, NSApplicationDelegate
{
static let instance = AppDelegate();
let coreData:CoreDataDelegate;
override init()
{
coreData = CoreDataDelegate.instance;
super.init();
}
...
}
Then in the storyboard I added an NSObject to my two table view controllers and set the base class to AppDelegate. And then bound the ArrayControllers to the moc via AppDelegate/coreData.moc.
Now only one instance of CoreDataDelegate is created (and therefore only one moc) and I can happily report that saving works now!

Core Data managed objects in swift - is nil allowed?

I have created NSManagedObject subclasses for my model objects in swift.
Typically my pattern is to create an instance of an object and then set properties on it, then save.
The new objects have properties that are set to nil. They are not optionals, though. I thought in swift this wasn't allowed?
A lot of times I need to check for values, but if I try something like:
if (managedObject.property == nil) I crash.
It seems Xcode doesn't automatically make managed vars optional when creating NSManagedObject subclasses. If the values are set as optional in the model, they should be optional in the subclass as well. (I set them as optional manually)
class ClassWithOptionalName: NSManagedObject {
#NSManaged var name: String?
}
Is managedObject.property optional value?
class CustomManagedObject: NSManagedObject {
#NSManaged var aProperty: String?
^
}

Resources