I have created a dataModel where I have a task. This task has several variables but also has a one-many link with events. These events could be things like pictures or comments being added and so there may be many events to one task. I have linked them up as shown below:
The entity files are also setup:
import CoreData
class Task: NSManagedObject {
@NSManaged var area : Area
@NSManaged var pictures : [TaskPicture]
@NSManaged var events : [Event]
@NSManaged var requestedByDept : NSString?
@NSManaged var requestedByUser : NSString?
@NSManaged var workloadDept : NSString?
@NSManaged var detailDescription : NSString?
@NSManaged var status : NSString
@NSManaged var uniqueId : NSString?
@NSManaged var isActive : NSNumber
@NSManaged var created : NSDate?
@NSManaged var modified : NSDate?
@NSManaged var thumbnail : NSData?
}
and
import CoreData
class Event: NSManagedObject {
@NSManaged var task : Task
@NSManaged var actionBy : NSString?
@NSManaged var detailDescription : NSString?
@NSManaged var status : NSString
@NSManaged var uniqueId : NSString?
@NSManaged var isActive : NSNumber
@NSManaged var created : NSDate?
@NSManaged var modified : NSDate?
@NSManaged var type : NSString
}
To me, these all look correct but i may be wrong. I then fetch all of the tasks in one of my viewControllers
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Task", inManagedObjectContext: self.managedObjectContext)
fetchRequest.entity = entity
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "created", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
println("Unresolved error \(error)")
abort()
}
return _fetchedResultsController!
}
I can assure you that the managedObjectContext is both up-to-date and passed in correctly. The problem then occurs if I try to access the events array. I fetch each of the tasks using
let foundTask : Task = self.fetchedResultsController.objectAtIndexPath(indexPath) as Task
using cellForRowAtIndexPath as it is in a tableView. This again works but then when i try let event = foundTask.events[0]
or something similar, i get a crash with the message
-[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance 0x17022e9c0
I have tried both casting the array as an NSArray and using the functions like .firstObject, and have also put it into an NSSet but it never lets me access the events. It seems like when the Tasks are fetched, the events are not, hence giving a data fault. I would expect it to fetch the events when i need them but again, it does not do so. Sorry for the long post, but i wanted to include as much information as I felt you would need to help me. Please ask if you need any more information and i will try to reply as quickly as possible.
Thanks a lot
A (unordered) to-many relationship in Core Data is represented as NSSet
, not as an array,
so the property should be declared as
@NSManaged var events: NSSet
To convert the set to an array use allObjects
:
let events = foundTask.events.allObjects as [Event]
Note that Xcode can create the managed object subclasses for you (Editor -> Create NSManagedObject Subclass...), which I would recommend to avoid this kind of error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With