Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Core Data with Swift 2 Inserting and Saving Data Using a Context The Save Method

Todo App Crashing

Hi Everyone,

I've come across a bit of a problem with my code for the Todo App. So far everything was coming along perfectly until now.

I've followed along with Pasan Premaratne and my code matches what he has done up until this point. I run the app, everything is good. The problem I'm faced with is when I go to create an item. When I click on the + the app crashes and I am presented with the following error on line 50 of the CoreDataStack.swift.

Thread 1: breakpoint 1.1

Here's the line of code that I have where this message is coming up.

public lazy var managedObjectContext: NSManagedObjectContext = {

A lot of the information I'm presented with is gibberish and there I am getting no errors. Here's the code I have entered in the CoreDataStack.swift file.

import Foundation
import CoreData

public class DataController: NSObject {

    static let sharedInstance = DataController()

    private override init() {}

    private lazy var applicationDocumentsDirectory: NSURL = {
       let urls = NSFileManager.defaultManager().URLsForDirectory(.DesktopDirectory, inDomains: .UserDomainMask)
        return urls[urls.endIndex.predecessor()]
    }()

    private lazy var managedObjectModel: NSManagedObjectModel = {
       let modelURL = NSBundle.mainBundle().URLForResource("TodoList", withExtension: "momd")!
        return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

    private lazy var persistantStoreCoordinator: NSPersistentStoreCoordinator = {
       let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("TodoList.sqlite")

        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)

        } catch {
            let userInfo: [String: AnyObject] = [
                NSLocalizedDescriptionKey: "Failed to Initialize the Application's Saved Data",
                NSLocalizedFailureReasonErrorKey: "There Was An Error Creating or Loading the Application's Saved Data",
                NSUnderlyingErrorKey: error as NSError
            ]

            let wrappedError = NSError(domain: "au.com.stucowley.CoreDataError", code: 9999, userInfo: userInfo)
            print("Unresolved Error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }

        return coordinator
    }()

    public lazy var managedObjectContext: NSManagedObjectContext = {
       let coordinator = self.persistantStoreCoordinator
        let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)

        managedObjectContext.persistentStoreCoordinator = coordinator

        return managedObjectContext
    }()

    public func saveContext() {
        if managedObjectContext.hasChanges {
            do {
                try managedObjectContext.save()
            } catch let error as NSError {
                print("Unresolved Error \(error), \(error.userInfo)")
            }
        }
    }
}

I do have 2 warning, but I noticed that in the videos Pasan has 2 warnings also, so I'm going to assume that mine would be the same.

Any help with this would be greatly appreciated

Thanks

Stu :)

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

It seems you have accidentally set a breakpoint on this line. That's indicated by a blue arrow above the line number. To add one, you simply click the line number. Clicking on it again will toggle the disabled state of this breakpoint. To remove it, drag and drop it to the left. You can also use the breakpoint navigator, as explained in Apple docs.

Hope that helps :)