简体   繁体   中英

CoreData creating an NSManagedObject Subclass with a Parent Entity

We have an app in Swift that has 10-12 data models. The developer who set them up generated them in the .xcdatamodeld file and I've since wrote extensions for them.

I've gotten to the point in the app where I realized that he had failed to generate a subclass for one of the data models named "Files". I tried adding it using the Editor -> Create NSManagedObject Subclass, and upon the wizard's completion, no error appears, but the subclass also does not appear. Assuming it might be a bug in XCode, I wrote my own Files subclass following the format of the others. Its pretty basic:

import Foundation
import CoreData

class Files: NSManagedObject {

    @NSManaged var localUrl: String
    @NSManaged var remoteUrl: String
    @NSManaged var type: String
    @NSManaged var translationFromAudio: NSSet
    @NSManaged var translationToAudio: NSSet
}

Following this I went along parsing the JSON for our project and pushing the data into coreData

let entityDescription: AnyObject = NSEntityDescription.entityForName("Files", inManagedObjectContext: context)!

var request = NSFetchRequest(entityName: "Files")
request.predicate = NSPredicate(format: "remoteUrl == %@", urlString)
var file:Files


var results = context.executeFetchRequest(request, error: nil)! as NSArray

if results.firstObject != nil{
    file = results.firstObject as Files
}else{
file = Files(entity: entityDescription as NSEntityDescription, insertIntoManagedObjectContext: context)
}

file.remoteUrl = audioString
file.type = "Institutional"
//All the other properties on a file are optional, so I did not include them

All works fine and dandy (this is the same format we used to save everything else into CoreData just with File's properties substituted in) The context.save() is ran after all parsing is complete to speed up the app, but it is called.

The crazy stuff starts happening when I try and run a FetchRequest to get all the Files from coreData. It comes back as 0 objects, but all the other coreData models come back.

I then tried deleting the app off the simulator and reloading it, and upon doing this after parsing again, nothing is saved to core data at all.

I've rolled back my code twice, and carefully did everything and the same result happens. I'm guessing this could be connected to the original issue of the Files subclass not generating properly. Any info on how to refresh your subclasses, or ideas on what I could be doing wrong are appreciated, I'm fairly new to Swift and XCode, so everything is still very new to me. Thanks!

EDIT

The solution below fixed my original problem, but I am left with two new issues:

  1. Why can't I auto-generate subclasses from the menu Editor->Create NSManagedObject Subclass.
  2. How should I format my subclass "Files" to inherit from a parent entity as the original functionality dictated.

So, I've fixed it, and with the fix created more questions.

In the entity editor, I selected the entity "Files", and in the Entity menu on the right hand side (the third tab on the righthand menu, I noticed that the Parent Entity Dropdown, had a parent entity selected, whereas all the others had "no parent entity". Upon doing this, deleting my app off the simulator and refreshing, functionality was restored and CoreData resumed its normal function.

This has opened two follow up questions: 1. Why can't I auto-generate subclasses from the menu Editor->Create NSManagedObject Subclass. 2. How should I format my subclass "Files" to inherit from a parent entity as the original functionality dictated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM