在swift ios中再次运行应用程序后核心数据显示故障记录?
Posted
技术标签:
【中文标题】在swift ios中再次运行应用程序后核心数据显示故障记录?【英文标题】:core data show fault records after again run the application in swift ios? 【发布时间】:2019-12-07 05:28:53 【问题描述】:我正在使用核心数据从服务器检索记录。首次启动时,插入和检索记录成功。但是再次停止并运行该应用程序我在我的应用程序中检索故障记录。我做错了什么?
持久化容器:
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
【问题讨论】:
【参考方案1】:只需在 fetch 方法中添加一行
fetchRequest.returnsObjectsAsFaults = false
【讨论】:
【参考方案2】:not working above the answer. this issue based on every time managed context deallocated. so fault records come in the second launch. I resolved using the below code.
import Foundation
import UIKit
import CoreData
final class CoreDataStack
static let sharedInstance = CoreDataStack()
private init()
// MARK: - Core Data stack
static 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
()
static var context: NSManagedObjectContext
return persistentContainer.viewContext
// MARK: - Core Data Saving support
static 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 CoreDataStack.context.fetch(fetchRequest) as? [T]
return fetchObjects ?? [T]()
catch
print(error)
return [T]()
extension CoreDataStack
func applicationDocumentsDirectory()
// The directory the application uses to store the Core Data store file. This code uses a directory named "yo.BlogReaderApp" in the application's documents directory.
if let url = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).last
print(url.absoluteString)
func get_Report_from_Json_in_create_report(field_jsonObj : NSDictionary) -> Report
let context = CoreDataStack.context
let reportEntity = Report(context: context)
reportEntity.r_ID = self.changeStringNumber(convertString: field_jsonObj.value(forKey: "reportid")!)
reportEntity.status = self.changeStringNumber(convertString: field_jsonObj.value(forKey: "status")!)
reportEntity.szDesc = field_jsonObj.value(forKey: "reportdesc") as? String
reportEntity.openBy = field_jsonObj.value(forKey: "openby")as? String
reportEntity.openByDateTime = field_jsonObj.value(forKey: "openBydatetime")as? String
reportEntity.closedBy = field_jsonObj.value(forKey: "closedBy")as? String
reportEntity.closedDateTime = field_jsonObj.value(forKey: "closeddatetime")as? String
reportEntity.assignedBy = field_jsonObj.value(forKey: "assignedby")as? String
reportEntity.assignedByDateTime = field_jsonObj.value(forKey: "assignedbydatetime")as? String
reportEntity.assignToUser = field_jsonObj.value(forKey: "assigntouser")as? String
reportEntity.assignToGroup = field_jsonObj.value(forKey: "assigntogroup")as? String
reportEntity.verifyBy = field_jsonObj.value(forKey: "verifyby")as? String
reportEntity.veriftyByDateTime = field_jsonObj.value(forKey: "veriftybydatetime")as? String
reportEntity.propertyID = self.changeStringNumber(convertString: field_jsonObj.value(forKey: "propertyid")!)
reportEntity.entityGroupID = self.changeStringNumber(convertString: field_jsonObj.value(forKey: "entitygroupid")!)
reportEntity.entityTypeID = self.changeStringNumber(convertString:field_jsonObj.value(forKey: "entitytypeid")!)
reportEntity.entityID = self.changeStringNumber(convertString:field_jsonObj.value(forKey: "entityid")!)
reportEntity.entityDescGroup = field_jsonObj.value(forKey: "entitydescgroup")as? String
reportEntity.areaid = self.changeStringNumber(convertString:field_jsonObj.value(forKey: "areaid")!)
reportEntity.locationID = self.changeStringNumber(convertString:field_jsonObj.value(forKey: "locationid")!)
reportEntity.locationDescGroup = field_jsonObj.value(forKey: "locationdescgroup")as? String
reportEntity.reportType = field_jsonObj.value(forKey: "reporttype")as? String
reportEntity.utcOpenDateTime = field_jsonObj.value(forKey: "utcopendatetime")as? String
reportEntity.utcAssignDateTime = field_jsonObj.value(forKey: "utcassigndatetime")as? String
reportEntity.utcVerifyDateTime = field_jsonObj.value(forKey: "utcverifydatetime")as? String
reportEntity.utcClosedDateTime = field_jsonObj.value(forKey: "utccloseddatetime")as? String
reportEntity.lastUpdateDatetime = field_jsonObj.value(forKey: "lastupdatedatetime")as? String
reportEntity.assignDesc = field_jsonObj.value(forKey: "assigndesc")as? String
reportEntity.closedDesc = field_jsonObj.value(forKey: "closeddesc")as? String
reportEntity.typeO = field_jsonObj.value(forKey: "TypeO")as? String
reportEntity.pathO = field_jsonObj.value(forKey: "PathO")as? String
reportEntity.typeA = field_jsonObj.value(forKey: "TypeA")as? String
reportEntity.pathA = field_jsonObj.value(forKey: "PathA")as? String
reportEntity.typeC = field_jsonObj.value(forKey: "TypeC")as? String
reportEntity.pathC = field_jsonObj.value(forKey: "PathC")as? String
reportEntity.reportBy = "server"
return reportEntity
【讨论】:
以上是关于在swift ios中再次运行应用程序后核心数据显示故障记录?的主要内容,如果未能解决你的问题,请参考以下文章
核心数据 NSPersistentStoreCoordinator 中止来电,SWIFT