핵심 데이터 대량

kerac2002

응용 프로그램이 시작될 때마다 웹 서비스에서 검색되는 25000 개의 레코드가 있습니다. 초기화 단계에서이 모든 항목을 핵심 데이터에 저장하고 싶습니다. 이 작업에 소요되는 시간은 약 20 분입니다. 모든 항목을 핵심 데이터에 더 빠르게 저장할 수 있습니까? sqlite 도구를 사용하여 sqlite 데이터베이스에 저장 한 다음 핵심 데이터를 인스턴스화하는 것이 좋습니다.

편집하다:

핵심 데이터에 저장하는 데 20 분.

func storeDevices(_ dataStack: DATAStack,value: [JSON]) -> Bool
{
    dataStack.performInNewBackgroundContext { backgroundContext in
    let entityDevices = NSEntityDescription.entity(forEntityName: "Devices", in: backgroundContext)

    for item in value
    {
        if !item["event_id"].stringValue.isEmpty
        {
            let id = item["id"].stringValue
            let predicate = NSPredicate(format: "%K == %@", "id", id)
            let typesFetchRequest = NSFetchRequest<Devices>(entityName: "Devices")
            typesFetchRequest.predicate = predicate
            do {
                let fetchedTypesResults = try backgroundContext.fetch(typesFetchRequest) as! [Devices]
                if (fetchedTypesResults.count > 0)
                {
                    let typeUpdate = fetchedTypesResults[0]
                    switch item["is_Deleted"].stringValue
                    {
                    case "true":
                        typeUpdate.setValue(true, forKey: "deletedStatus")
                    case "false":
                        typeUpdate.setValue(false, forKey: "deletedStatus")
                    default:
                        typeUpdate.setValue(false, forKey: "deletedStatus")
                    }

                    if let intVersion = Int(item["last_modified"].stringValue) {
                        typeUpdate.setValue(intVersion, forKey: "lastModified")
                    } else {
                        typeUpdate.setValue(NSNumber(value: 0 as Int32), forKey: "lastModified")
                    }

                    typeUpdate.setValue(NSNumber(value: 0 as Int32), forKey: "syncStatus")

                    typeUpdate.setValue(item["deviceUUID"].stringValue, forKey: "deviceUUID")
                    typeUpdate.setValue(item["deviceLocation"].stringValue, forKey: "deviceLocation")

                    let event_id = item["event_id"].stringValue
                    if !event_id.isEmpty
                    {

                        let predicate = NSPredicate(format: "%K == %@", "id", event_id)
                        let typesFetchRequest = NSFetchRequest<Events>(entityName: "Events")
                        typesFetchRequest.predicate = predicate

                        do {
                            let fetchedTypesResults = try backgroundContext.fetch(typesFetchRequest) as! [Events]
                            if (fetchedTypesResults.count > 0)
                            {
                                typeUpdate.setValue(fetchedTypesResults[0], forKey: "event")
                            }
                            else
                            {
                                continue
                            }
                        } catch {
                            let saveError = error as NSError
                            print("Failed to fetch events in Devices: \(saveError)")
                        }
                    }


                    continue
                }

            } catch {
                fatalError("Failed to fetch devices: \(error)")
            }

            let deviceNewElement = Devices(entity: entityDevices!, insertInto: backgroundContext)
            deviceNewElement.id = id
            switch item["is_Deleted"].stringValue
            {
            case "true":
                deviceNewElement.deletedStatus = true
            case "false":
                deviceNewElement.deletedStatus = false
            default:
                deviceNewElement.deletedStatus = false
            }

            if let intVersion = Int(item["last_modified"].stringValue) {
                deviceNewElement.lastModified = intVersion as NSNumber?
            } else {
                deviceNewElement.lastModified = NSNumber(value: 0 as Int32)
            }

            deviceNewElement.deviceUUID = item["deviceUUID"].stringValue
            deviceNewElement.deviceLocation = item["deviceLocation"].stringValue

            let event_id = item["event_id"].stringValue
            if !event_id.isEmpty
            {
                let predicate = NSPredicate(format: "%K == %@", "id", event_id)
                let typesFetchRequest = NSFetchRequest<Events>(entityName: "Events")
                typesFetchRequest.predicate = predicate

                do {
                    let fetchedTypesResults = try backgroundContext.fetch(typesFetchRequest) as! [Events]
                    if (fetchedTypesResults.count > 0)
                    {
                        deviceNewElement.event = fetchedTypesResults[0]
                    }
                    else
                    {
                        continue
                    }
                } catch {
                    let saveError = error as NSError
                    print("Failed to fetch events in Devices: \(saveError)")
                }
            }

            deviceNewElement.syncStatus = NSNumber(value: 0 as Int32)
        }
    }   
        try! backgroundContext.save()
    }
    return true

}
사냥꾼

CoreData 사용 방법을 최적화해야합니다.

데이터를 처리하고 어떤 ManagedObjects를 터치해야하는지 파악하고 루프 외부에서 대량으로 미리 가져옵니다. 프리 페칭이 필요할 수도있는 관계를주의하십시오. 결함이 지속적으로 발생하는 것을 원하지 않습니다.

이제 모든 변경을 수행하십시오. 마지막에 한 번 저장하십시오.

메모리 부족으로 인해 데이터를 수천 개의 청크로 분할하고 위와 같이 처리하십시오. @autoreleasepool을 사용하십시오.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Swift-핵심 데이터 용량

분류에서Dev

핵심 데이터 일대 다 관계

분류에서Dev

핵심 데이터-대다 관계 속성에 대한 정보

분류에서Dev

NSJSONSerialization-JSON에 대한 핵심 데이터 관계

분류에서Dev

핵심 데이터 다 대다 술어 (다시)

분류에서Dev

다 대다 관계에서 핵심 데이터 검색

분류에서Dev

iCloud 및 핵심 데이터에 대한 NSPersistentStoreUbiquitousContentNameKey의 고유성

분류에서Dev

핵심 데이터-대다 관계에서 개체 순서

분류에서Dev

iOS 핵심 데이터 일대 다 관계

분류에서Dev

핵심 데이터 일대 다 역관계

분류에서Dev

핵심 데이터-일대 다 관계로 값 할당

분류에서Dev

Swift 3를 사용한 핵심 데이터에 대한 지침

분류에서Dev

awakeFromInsert 대 핵심 데이터의 기본값 설정

분류에서Dev

핵심 데이터-경량 마이그레이션이 작동하지 않음

분류에서Dev

핵심 데이터 @FetchRequest SwiftUI

분류에서Dev

핵심 데이터 및 didSelectRowAtIndexPath

분류에서Dev

핵심 데이터 기초

분류에서Dev

핵심 데이터와 UIActivityViewController

분류에서Dev

NSUserdefault 및 핵심 데이터

분류에서Dev

핵심 데이터 조건

분류에서Dev

핵심 데이터 개체 변경에 대한 SwiftUI 업데이트보기

분류에서Dev

iOS : 일대 다 관계가있는 핵심 데이터 업데이트 개체

분류에서Dev

맨틀 및 핵심 데이터-삭제 / 생성 대신 업데이트

분류에서Dev

새로운 핵심 데이터 xcode 8.3 메서드 대신 앱 위임 이전 핵심 데이터 메서드를 대체 할 수 있습니까?

분류에서Dev

핵심 데이터 다 대다 관계에서 중복 레코드 허용

분류에서Dev

대다 관계가있는 복잡한 핵심 데이터 술어

분류에서Dev

핵심 데이터에서 일대 다 관계를 처리하는 방법

분류에서Dev

Swift : 객체에 대한 핵심 데이터 NSSet을 확인하는 방법

분류에서Dev

일대 다 관계에서 항목을 제거하는 Swift 핵심 데이터

Related 관련 기사

  1. 1

    Swift-핵심 데이터 용량

  2. 2

    핵심 데이터 일대 다 관계

  3. 3

    핵심 데이터-대다 관계 속성에 대한 정보

  4. 4

    NSJSONSerialization-JSON에 대한 핵심 데이터 관계

  5. 5

    핵심 데이터 다 대다 술어 (다시)

  6. 6

    다 대다 관계에서 핵심 데이터 검색

  7. 7

    iCloud 및 핵심 데이터에 대한 NSPersistentStoreUbiquitousContentNameKey의 고유성

  8. 8

    핵심 데이터-대다 관계에서 개체 순서

  9. 9

    iOS 핵심 데이터 일대 다 관계

  10. 10

    핵심 데이터 일대 다 역관계

  11. 11

    핵심 데이터-일대 다 관계로 값 할당

  12. 12

    Swift 3를 사용한 핵심 데이터에 대한 지침

  13. 13

    awakeFromInsert 대 핵심 데이터의 기본값 설정

  14. 14

    핵심 데이터-경량 마이그레이션이 작동하지 않음

  15. 15

    핵심 데이터 @FetchRequest SwiftUI

  16. 16

    핵심 데이터 및 didSelectRowAtIndexPath

  17. 17

    핵심 데이터 기초

  18. 18

    핵심 데이터와 UIActivityViewController

  19. 19

    NSUserdefault 및 핵심 데이터

  20. 20

    핵심 데이터 조건

  21. 21

    핵심 데이터 개체 변경에 대한 SwiftUI 업데이트보기

  22. 22

    iOS : 일대 다 관계가있는 핵심 데이터 업데이트 개체

  23. 23

    맨틀 및 핵심 데이터-삭제 / 생성 대신 업데이트

  24. 24

    새로운 핵심 데이터 xcode 8.3 메서드 대신 앱 위임 이전 핵심 데이터 메서드를 대체 할 수 있습니까?

  25. 25

    핵심 데이터 다 대다 관계에서 중복 레코드 허용

  26. 26

    대다 관계가있는 복잡한 핵심 데이터 술어

  27. 27

    핵심 데이터에서 일대 다 관계를 처리하는 방법

  28. 28

    Swift : 객체에 대한 핵심 데이터 NSSet을 확인하는 방법

  29. 29

    일대 다 관계에서 항목을 제거하는 Swift 핵심 데이터

뜨겁다태그

보관