0

I am trying to fetch core data entities and am having a problem which I think is due to my use of a subclassed NSManagedObject.

Attempting to print out one of the fetched objects I get the following error:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

Here is my "Show" NSManagedObject Subclass:

import Foundation
import CoreData

@objc(Show)
class Show: NSManagedObject {

    @NSManaged var dateUpdated: NSDate
    @NSManaged var fontFilename: String
    @NSManaged var fontName: String
    @NSManaged var idShow: NSNumber
    @NSManaged var isActive: NSNumber
    @NSManaged var productIdentifier: String
    @NSManaged var title: String

}

Here is my fetching call in my helper class CoreDataManager.swift:

func fetchShows() -> Array<AnyObject>?
{
    // This function will fetch data for all available shows.
    // Return value will be an array of dictionaries (i.e. [0]->{Show, Characters})
    // i.e. Show Title, Store Product Identifier, Font, Characters, etc.
    var results = Array<AnyObject>()

    let context:NSManagedObjectContext = self.managedObjectContext!
    var error: NSError?
    var fetchRequest = NSFetchRequest()
    let entity = NSEntityDescription.entityForName("Show", inManagedObjectContext: context)
    fetchRequest.entity = entity

    let fetchedObjects = context.executeFetchRequest(fetchRequest, error: &error)! as Array
    for item in fetchedObjects
    {
        var showData = Dictionary<String, AnyObject>()
        let show: Show = item as Show
        showData.updateValue(show, forKey: "show")

        // Get Characters
        showData.updateValue(self.fetchCharactersForShow(show.idShow)!, forKey: "characters")
        results.append(showData)

        // Test
        self.printShow(show.idShow) //NOTE - THIS METHOD CREATES A NEW FETCH REQUEST USING THE PROVIDED SHOW ID AND SUCCESSFULLY PRINTS OUT THE SHOW DATA
    }
    return results
}

Here is where I am trying to now access the fetched values from RootViewController.swift:

    //TESTING
    self.shows = CoreDataManager().fetchShows()!
    let showData = self.shows[0] as Dictionary<String, AnyObject>
    let show = showData["show"] as Show
    println("TEST: Show \(show.title) is active: \(show.isActive.boolValue)")
    /******/

Here is a screenshot of the debugger for the 'Show' object: enter image description here

1 Answer 1

0

Wow I feel so dumb. It seems the problem was the way I was calling the 'fetchShows()' method. I needed to actually create and allocate a 'CoreDataManager' object and call the method using that object, instead of trying to just make a call to the class method:

let DataManager = CoreDataManager()
self.shows = DataManager.fetchShows()

And now it works!

Sign up to request clarification or add additional context in comments.

1 Comment

It's a mystery to me why this happens when the debugger happily shows the object, but I take it that by not "retaining" the model object, the backing storage for the entity objects is scrapped before they get properly awoken (in my case one of the properties is transformable so it makes sense that it wouldn't be built until needed). You may need it to hang around until some undefined point in future though, so using a lazy property to keep the owning object may be the way to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.