【问题标题】:Core data is taking long time to insert data into database核心数据需要很长时间才能将数据插入数据库
【发布时间】:2016-08-17 06:36:33
【问题描述】:

您好,我有全球城市的数据,即 127960 个城市。我正在尝试将此数据插入应用程序数据库。大约需要 20 分钟。

//Here is code

   private func FetchCountryList(){

        var parameters = [String:AnyObject]()

        if let rDate = Country.GetLatestDate() as NSNumber?{
            parameters[RECORDED_DATE] = rDate.integerValue
        }

        WSRequest.SendRequest(WSMethod.POST, pramrDisc: parameters, paramsString:nil, operation: WSOperation.FetchCountryList, completionHandler: {response in

            if let parsedObject = response.parsedObject as? [String:AnyObject]{

                if let countries = parsedObject[OBJECT1]?[COUNTRIES] as? [[String:AnyObject]]{

                   let managedContext = CoreDataStack.sharedStack().backgroundContext

                    managedContext.performBlock({

                        for country in countries {
                            AddCountryInManagedObjectContext(country)
                        }

                        managedContext.saveContext()
                        self.completedDataBurning()

                    })
                }
            }
        })
    }

class func AddCountryInManagedObjectContext(user:[String:AnyObject]) {

    if let countryId = user[COUNTRY_ID] as? Int{

        let backgroundContext = CoreDataStack.sharedStack().backgroundContext
        backgroundContext.performBlockAndWait({

            let newItem = DatabaseManager.CreateOrUpdateItemFor(backgroundContext,entity: TABLE_COUNTRY, parameter: "countryId", value: countryId) as! Country

            newItem.countryId = countryId

            if let countryName = user[COUNTRY_NAME] as? String{
                newItem.countryName = countryName.capitalizedString
            }

            if let currency = user[CURRENCY] as? String{
                newItem.currency = currency.uppercaseString
            }

            if let currencyCode = user[CURRENCY_CODE] as? Int{
                newItem.currencyCode = currencyCode
            }

            if let currencySymbol = user[CURRENCY_SYMBOL] as? String{
                newItem.currencySymbol = currencySymbol
            }

            if let recordedBy = user[RECORDED_BY] as? String{
                newItem.recordedBy = recordedBy.capitalizedString
            }

            if let recordedDate = user[RECORDED_DATE] as? Double{
                newItem.recordedDate = recordedDate
            }

            if let status = user[STATUS] as? String{
                if status == "D"{
                    DeleteRecord(backgroundContext,entityName: TABLE_COUNTRY, columnName: "countryId", recordId: "\(countryId)")
                }
            }

        })

    }
}

class func DeleteRecord(managedContext:NSManagedObjectContext,entityName:String,columnName:String,recordId:String) {


    managedContext.performBlockAndWait({

        //2
        let fetchRequest = NSFetchRequest(entityName:entityName)
        fetchRequest.includesPropertyValues = false

        let predicateFormat = "\(columnName) = \(recordId)"

        let resultPredicate = NSPredicate(format: predicateFormat)
        fetchRequest.predicate = resultPredicate

        //3
        //var error: NSError?

        do {

            if let results = try managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]{

                for result in results{
                    managedContext.deleteObject(result)
                }
            }

            try managedContext.save()

        } catch let error as NSError {
            print(error)
        }
    })
}
    //Core data stack

        import CoreData

        let coreDataStack = CoreDataStack()

        class CoreDataStack {
        class func sharedStack() -> CoreDataStack{
            return coreDataStack
        }

        // MARK: - Core Data stack

        lazy var applicationDocumentsDirectory: NSURL = {
            // The directory the application uses to store the Core Data store file. This code uses a directory named "in.appstute.TripOrb" in the application's documents Application Support directory.
            let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
            return urls[urls.count-1]
        }()

        lazy var managedObjectModel: NSManagedObjectModel = {
            // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
            let modelURL = NSBundle.mainBundle().URLForResource("TripOrb", withExtension: "momd")!
            return NSManagedObjectModel(contentsOfURL: modelURL)!
        }()

        lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
            // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
            // Create the coordinator and store
            let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
            let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
            var failureReason = "There was an error creating or loading the application's saved data."
            do {
                try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
            } catch {
                // Report any error we got.
                var dict = [String: AnyObject]()
                dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
                dict[NSLocalizedFailureReasonErrorKey] = failureReason

                dict[NSUnderlyingErrorKey] = error as NSError
                let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
                // Replace this with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
                abort()
            }

            return coordinator
        }()

        lazy var context: NSManagedObjectContext = {
            // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
            let coordinator = self.persistentStoreCoordinator
            var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
            managedObjectContext.persistentStoreCoordinator = coordinator
            return managedObjectContext
        }()

        lazy var backgroundContext: NSManagedObjectContext = {
            // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
            let coordinator = self.persistentStoreCoordinator
            var managedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
    //        managedObjectContext.persistentStoreCoordinator = coordinator
            managedObjectContext.parentContext = self.context

            return managedObjectContext
        }()

        // MARK: - Core Data Saving support

        func saveContext () {
            if context.hasChanges {
                do {
                    try context.save()
                } catch {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    let nserror = error as NSError
                    NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                    abort()
                }
            }
        }

        func saveBackgroundContext () {
            if backgroundContext.hasChanges {
                do {
                    try backgroundContext.save()
                } catch {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    let nserror = error as NSError
                    NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                    abort()
                }
            }
        }
    }

    extension NSManagedObjectContext {
        func saveContext () {
            if self.hasChanges {
                do {
                    try self.save()
                } catch {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    let nserror = error as NSError
                    NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                    abort()
                }
            }
        }
    }

【问题讨论】:

  • 您是否使用仪器来查看您要求核心数据做什么?阅读有关数据加载的任何其他答案?
  • 使用直接sqlite处理大数据,毕竟核心数据是sqlite和程序员之间的中间层,不支持多线程处理..
  • @vaibhav 从什么时候开始 SQLite 在批量插入情况下从多个线程中获利?顺序批量插入似乎是显而易见的解决方案。
  • 很好,您可以使用自己的解决方案..
  • 为什么要创建两个背景上下文,看起来有点乱?只在一个执行块上运行它不是更好吗?

标签: ios swift multithreading performance core-data


【解决方案1】:

查看核心数据编程指南中的Efficiently Importing Data 章节。

尝试批量大小为 1000,这似乎是最佳值。

【讨论】:

  • 如果你能在那个链接找到那一章,你能描述一下在哪里可以找到它吗?我知道曾经有一个版块叫这个名字,但现在我找不到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2016-01-17
相关资源
最近更新 更多