开发者社区> 问答> 正文

核心数据显示故障记录后,再次运行应用程序在快速iOS?

我使用核心数据从服务器检索记录。当第一次启动时,成功地插入和检索记录.但是,停止运行应用程序,我在我的应用程序中检索故障记录。我做错什么了?

持久化集装箱:

static let sharedInstance = CoreDataStack()
    private init() {}

    // MARK: - Core Data stack
    lazy var persistentContainer: NSPersistentContainer = {

        let container = NSPersistentContainer(name: "BIDATA")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    lazy var context = persistentContainer.viewContext

    // MARK: - Core Data Saving support

    func save() {
    //    let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
                print("saved successfully")
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }


    func fetch<T: NSManagedObject>(_ objectType: T.Type) -> [T]
    {
        let entityName = String(describing: objectType)
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
        do{
            let fetchObjects = try context.fetch(fetchRequest) as? [T]
            return fetchObjects ?? [T]()
        }catch
        {
            print(error)
            return [T]()
        }
    }
}



func get_Report_from_Json(field_jsonObj : NSDictionary) -> Bool {

        var returnBool = false
        let context = CoreDataStack.sharedInstance.persistentContainer.viewContext


        let reportID = helper.changeStringNumber(convertString:field_jsonObj.value(forKey: "ID")!)

        let Status = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "Status")!)
        let szDesc = field_jsonObj.value(forKey: "szDesc")
        let OpenBy = field_jsonObj.value(forKey: "OpenBy")
        let OpenByDateTime = field_jsonObj.value(forKey: "OpenByDateTime")

        let ClosedBy = field_jsonObj.value(forKey: "ClosedBy")        
        let ClosedDateTime = field_jsonObj.value(forKey: "ClosedDateTime")

        let AssignedBy = field_jsonObj.value(forKey: "AssignedBy")

        let AssignedByDateTime = field_jsonObj.value(forKey: "AssignedByDateTime")

        let AssignToUser = field_jsonObj.value(forKey: "AssignToUser")

        let AssignToGroup = field_jsonObj.value(forKey: "AssignToGroup")

        let VerifyBy = field_jsonObj.value(forKey: "VerifyBy")

        let VeriftyByDateTime = field_jsonObj.value(forKey: "VeriftyByDateTime")

        let PropertyID =  helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "PropertyID")!)

        let EntityGroupID = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "EntityGroupID")!)

        let EntityTypeID = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "EntityTypeID")!)

        let EntityID = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "EntityID")!)

        let EntityDescGroup = field_jsonObj.value(forKey: "EntityDescGroup")

        let AreaID = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "AreaID")!)

        let LocationID = helper.changeStringNumber(convertString: field_jsonObj.value(forKey: "LocationID")!)

        let LocationDescGroup = field_jsonObj.value(forKey: "LocationDescGroup")

        let ReportType = field_jsonObj.value(forKey: "ReportType")

        let UTCOpenDateTime = field_jsonObj.value(forKey: "UTCOpenDateTime")

        let UTCAssignDateTime = field_jsonObj.value(forKey: "UTCAssignDateTime")

        let UTCVerifyDateTime = field_jsonObj.value(forKey: "UTCVerifyDateTime")

        let UTCClosedDateTime = field_jsonObj.value(forKey: "UTCClosedDateTime")

        let LastUpdateDatetime = field_jsonObj.value(forKey: "LastUpdateDatetime")

        let AssignDesc = field_jsonObj.value(forKey: "AssignDesc")

        let ClosedDesc = field_jsonObj.value(forKey: "ClosedDesc")
        let TypeO = field_jsonObj.value(forKey: "TypeO") is NSNull ? "" : field_jsonObj.value(forKey: "TypeO")

        let PathO = field_jsonObj.value(forKey: "PathO")is NSNull ? "" : field_jsonObj.value(forKey: "PathO")

        let TypeA = field_jsonObj.value(forKey: "TypeA")is NSNull ? "" : field_jsonObj.value(forKey: "TypeA")

        let PathA = field_jsonObj.value(forKey: "PathA")is NSNull ? "" : field_jsonObj.value(forKey: "PathA")

        let TypeC = field_jsonObj.value(forKey: "TypeC")is NSNull ? "" : field_jsonObj.value(forKey: "TypeC")

        let PathC = field_jsonObj.value(forKey: "PathC")is NSNull ? "" : field_jsonObj.value(forKey: "PathC")

        let ReportBy = "server"

        let photoEntity = Report(context: persistenceManager.context)
        photoEntity.r_ID = reportID
        photoEntity.propertyID = PropertyID
        photoEntity.szDesc = szDesc as? String
        photoEntity.status = "1"
        photoEntity.openBy = OpenBy as? String
        photoEntity.openByDateTime = OpenByDateTime as? String
        photoEntity.closedBy = ClosedBy as? String
        photoEntity.closedDateTime = ClosedDateTime as? String
        photoEntity.assignedBy = AssignedBy as? String
        photoEntity.assignedByDateTime = AssignedByDateTime as? String
        photoEntity.assignToUser = AssignToUser as? String
        photoEntity.assignToGroup = AssignToGroup as? String
        photoEntity.verifyBy = VerifyBy as? String
        photoEntity.veriftyByDateTime = VeriftyByDateTime as? String
        photoEntity.entityGroupID = EntityGroupID
        photoEntity.entityTypeID = EntityTypeID
        photoEntity.entityID = EntityID
        photoEntity.entityDescGroup = EntityDescGroup as? String
        photoEntity.areaid = AreaID
        photoEntity.locationID = LocationID
        photoEntity.locationDescGroup = LocationDescGroup as? String
        photoEntity.reportType = ReportType as? String
        photoEntity.utcOpenDateTime = UTCOpenDateTime as? String
        photoEntity.utcAssignDateTime = UTCAssignDateTime as? String
        photoEntity.utcVerifyDateTime = UTCVerifyDateTime as? String
        photoEntity.utcClosedDateTime = UTCClosedDateTime as? String
        photoEntity.lastUpdateDatetime = LastUpdateDatetime as? String
        photoEntity.assignDesc = AssignDesc as? String
        photoEntity.closedDesc = ClosedDesc as? String
        photoEntity.typeO = TypeO as? String
        photoEntity.pathO = PathO as? String
        photoEntity.typeA = TypeA as? String
        photoEntity.pathA = PathA as? String
        photoEntity.typeC = TypeC as? String
        photoEntity.pathC = PathC as? String
        photoEntity.reportBy = ReportBy

        persistenceManager.save()


        return true

}



func get_Report_from_database() -> [Report]?{

        let reportList = persistenceManager.fetch(Report.self)
        return reportList
}

展开
收起
游客5akardh5cojhg 2019-12-07 19:02:49 468 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载