gpt4 book ai didi

ios - 传递 NSManagedObjectContext 的实例

转载 作者:行者123 更新时间:2023-11-28 07:09:45 25 4
gpt4 key购买 nike

我使用 MagicalRecord使用核心数据。

我有一个名为 DatabaseManager 的单独类来处理与核心数据相关的任务。我有一个接受 NSManagedObjectContext 对象的初始化程序。

import CoreData
import Foundation
import MagicalRecord
import SwiftyJSON

public class DatabaseManager {

private let context: NSManagedObjectContext!

init(context: NSManagedObjectContext) {
self.context = context
}

public func insertReports(data: AnyObject, success: () -> Void, failure: (error: NSError?) -> Void) {
MagicalRecord.cleanUp()
MagicalRecord.setupCoreDataStackWithStoreNamed("Records")

let json = JSON(data)
if let reports = json.array {
for reportObj in reports {
let report = Report.MR_createInContext(context) as Report
report.id = reportObj["Id"].int
report.type = reportObj["Type"].string!
}
context.MR_saveToPersistentStoreAndWait()
success()
}
}

private func getReport(id: Int) -> Report {
let idFilter = NSPredicate(format: "id == %@", NSNumber(integer: id))
let fetchRequest = Report.MR_requestAllWithPredicate(idFilter, inContext: context)
return Report.MR_executeFetchRequestAndReturnFirstObject(fetchRequest, inContext: context) as Report
}
}

然后我初始化该类并调用它的 insertReports() 方法将一些记录插入到核心数据中。

let context = NSManagedObjectContext.MR_defaultContext()
let dbManager = DatabaseManager(context: context)

getReportsFromAPI({ (data) -> Void in
dbManager.insertReports(data, success: { () -> Void in
println("Reports added successfully")
}, failure: { (error) -> Void in
println("Error inserting Reports: \(error?.localizedDescription)")
})
}, failure: { (error) -> Void in
println("Error getting Reports from API: \(error?.localizedDescription)")
})

但这似乎行不通。我在控制台中得到以下输出。

2015-03-05 00:39:39.412 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x104396400) Set Default Context: (null)
2015-03-05 00:39:39.412 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x104396400) Set Root Saving Context: (null)
2015-03-05 00:39:39.583 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x104396400) -> Created Context UNNAMED
2015-03-05 00:39:39.584 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x104396400) Set Root Saving Context: <NSManagedObjectContext: 0x7f81b04c6b00>
2015-03-05 00:39:39.584 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x104396400) Created Main Queue Context: <NSManagedObjectContext: 0x7f81b04c75a0>
2015-03-05 00:39:39.584 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x104396400) Set Default Context: <NSManagedObjectContext: 0x7f81b04c75a0>
2015-03-05 00:39:40.434 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x104396400) Set Default Context: (null)
2015-03-05 00:39:40.434 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x104396400) Set Root Saving Context: (null)
2015-03-05 00:39:40.435 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x104396400) Set Default Context: (null)
2015-03-05 00:39:40.435 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x104396400) Set Root Saving Context: (null)
2015-03-05 00:39:40.443 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x104396400) -> Created Context UNNAMED
2015-03-05 00:39:40.443 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x104396400) Set Root Saving Context: <NSManagedObjectContext: 0x7f81b280e5a0>
2015-03-05 00:39:40.444 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x104396400) Created Main Queue Context: <NSManagedObjectContext: 0x7f81b04d76c0>
2015-03-05 00:39:40.444 Reports[7123:967179] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x104396400) Set Default Context: <NSManagedObjectContext: 0x7f81b04d76c0>
2015-03-05 00:39:40.446 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c75a0) → Saving <NSManagedObjectContext (0x7f81b04c75a0): *** DEFAULT ***> on *** MAIN THREAD ***
2015-03-05 00:39:40.446 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c75a0) → Save Parents? 1
2015-03-05 00:39:40.447 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c75a0) → Save Synchronously? 1
2015-03-05 00:39:40.448 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c6b00) → Saving <NSManagedObjectContext (0x7f81b04c6b00): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2015-03-05 00:39:40.448 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c6b00) → Save Parents? 1
2015-03-05 00:39:40.448 Reports[7123:967179] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x7f81b04c6b00) → Save Synchronously? 1

在那之后,它就卡住了。没有抛出错误,但保存操作也没有完成。

我用 NSManagedObjectContext.MR_defaultContext() 替换了我使用属性上下文的地方,然后运行应用程序,数据成功保存。

为什么我不能通过初始化器传入 MR_defaultContext() 的实例?

最佳答案

您的回溯说明了一切,您设置了太多与持久存储的连接。魔法唱片 documentation详细说明您应该在 App Delegate 的 didFinishLaunching 阶段,或者在您的情况下,在实例化 DatabaseManager 类时设置您的堆栈。

因此对 MagicalRecord.setupCoreDataStackWithStoreNamed("Records") 的调用应该转移到您的初始化程序(将其放在 dispatch_once block 中可能更安全,以防止它被设置多次)

init(context: NSManagedObjectContext) {
struct Static {
static var onceToken : dispatch_once_t = 0
}
dispatch_once(&(Static.onceToken)) {
MagicalRecord.setupCoreDataStackWithStoreNamed("Records")
}
self.context = context
}

顺便说一句,你只需要在你的应用程序关闭时调用清理,所以也许在你的 App Delegate 的 willTerminate

MagicalRecord 一切顺利 :)

关于ios - 传递 NSManagedObjectContext 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com