Search code examples
iosswiftcore-datarealm

App freezes and terminates because of memory usage when fetching Core Data objects


I have an iOS application where Core Data has worked for months, and at some point after I upgraded to High Sierra, Xcode 9/Swift 4 and iOS 11, it stopped working. I don't work on it daily as it's a side project, so I can't pinpoint exactly when it stopped working.

When my application launches I fetch all Project objects (part of my Core Data model) in viewDidLoad and put them in a projects property in my view controller. I fetch with the following method

func fetchProjects() -> [Project] {
    let fetchRequest = Project.fetchRequest() as! NSFetchRequest<Project>
    var projects: [Project] = []

    do {
        projects = try coreDataStack.viewContext.fetch(fetchRequest)
    } catch {
        print(error.localizedDescription)
    }

    return projects
}

The problem is, the application just goes white and starts eating up the memory of the device and then quits with only a message that says Message from debugger: Terminated due to memory issue. If I remove the call to this method, it launches just fine.

I uploaded the application to TestFlight and a friend says it's working fine, so it's most likely a problem at my end, but I have no idea what.

Update 1 The problem with doing Instruments is that Instruments Core Data only work on a simulator, and my problems only appear when running it on my device. When I run it on the simulator, though, I don't see anything that I interpret as an error.

I tried setting -com.apple.CoreData.SQLDebug 1 as an argument to the application, but this doesn't give me anything fishy either.

I have added my Core Data Stack class so you can see how I set it up, though it's pretty standard.

final class CoreDataStack {

    // MARK: Properties
    static let sharedInstance = CoreDataStack()

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: Strings.CoreData.modelName)
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    lazy var viewContext: NSManagedObjectContext = {
        return self.persistentContainer.viewContext
    }()

    // MARK: Initialization
    private init() {
    }

    // MARK: - Saving
    func saveContext() {
        guard persistentContainer.viewContext.hasChanges else {
            return
        }

        do {
            try persistentContainer.viewContext.save()
        } catch let error as NSError {
            print("Unresolved error \(error), \(error.userInfo)")
        }
    }
}

Update 2 The weird thing is, Realm showed similar behavior. It wouldn't let me fetch Projects. Maybe something underlying is the problem?


Solution

  • Well, it seems I fixed it by doing a clean install of my device. I haven't discovered yet why the error occurs, but anyway, that's the fix.