Skip to content

ImportableUniqueObject take long time (on the first load) #502

@loicleser

Description

@loicleser

Hello,

I'm testing your solution in my app. Everything looks really good and can be very helpful for me.

However, I'm encountering issues with the loading of the data. I'm trying to load a large set of movies from a JSON (40,000 movies). Everything works well with ImportableObject, but it duplicates the objects on each download. So, I implemented ImportableUniqueObject, and it takes a very long time to save the data.

With ImportableObject, it takes 1 or 2 seconds, and with ImportableUniqueObject, it takes 4 to 5 minutes.

Am I doing something wrong?

Here is my code:

class CoreDataManager {
    
    static let shared = CoreDataManager()
    
    let dataStack = DataStack(
        xcodeModelName: "___",
        migrationChain: []
    )
    
    init() {
        do {
            try dataStack.addStorageAndWait(
                SQLiteStore(
                    fileName: "MyDataModel.sqlite",
                    configuration: "Default",
                    localStorageOptions: .recreateStoreOnModelMismatch
                )
            )
        } catch {
            print("Error configuring CoreStore: \(error)")
        }
    }
}
func downloadMovies() {
    let url = URL(string: "MYJSONURL")!
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let data = data, error == nil else {
            print("error: \(error)")
            return
        }

        do {
            let decoder = JSONDecoder()
            let movies = try decoder.decode([MovieJSON].self, from: data)
            self.dataStack.perform(
                asynchronous: { (transaction) -> Void in
                    try! transaction.importUniqueObjects(
                        Into<Movie>(),
                        sourceArray: movies
                    )
                },
                completion: { (result) -> Void in
                    switch result {
                    case .success:
                        print("success")
                    case .failure(let error):
                        print("error: \(error)")
                    }
                }
            )
        } catch {
            print("error: \(error)")
        }
    }
    task.resume()
}

class Movie: NSManagedObject, ImportableUniqueObject {
    static func uniqueID(from source: MovieJSON, in transaction: CoreStore.BaseDataTransaction) throws -> String? {
        return source.name
    }
    
    func update(from source: MovieJSON, in transaction: CoreStore.BaseDataTransaction) throws {
        
    }
    
    func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws {
        self.name = source.name
    }
    
    typealias UniqueIDType = String
    
    static var uniqueIDKeyPath: String {
        return #keyPath(Movie.name)
    }
    
    typealias ImportSource = MovieJSON
    
}

Thank you for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions