我已经用3 x iPhone 5、5s,6、6s和7测试了我的代码。

我仅在所有iPhone 5设备上收到上述错误。不知道这是怎么回事,但也许5是32位设备这一事实可能是个线索?

我正在从viewcontroller类中调用以下方法

func startRecording() {
    disableControls()

    CoreDataStack.shared.performForegroundTask { (context) in
            let sessionInfo = SessionInfo(context: context)
            sessionInfo.startTime = Date().timeIntervalSince1970
            sessionInfo.userId = self.config.userId
            sessionInfo.devicePosition = self.config.devicePosition.rawValue
            sessionInfo.deviceType = self.config.deviceType.rawValue
            sessionInfo.deviceNumber = self.config.deviceNumber
            sessionInfo.deviceSide = self.config.deviceSide.rawValue

            do {
                    try context.obtainPermanentIDs(for: [sessionInfo])
            } catch {
                    print("Error obtaining permanent ID for session info record")
                    return
            }

            CoreDataStack.shared.saveViewContextAndWait()

            DispatchQueue.main.async {
                    guard sessionInfo.objectID.isTemporaryID == false else {
                            print("ObjectID is temporary")
                            return
                    }

                    self.recording = true
                    self.statusLabel.text = "Recording..."
                    self.recordManager.start(sessionUID: sessionInfo.uid)
            }
    }
}

config变量是一个简单的结构:
struct Configuration {
    var userId: String = "Unknown"
    var deviceType: DeviceType = .phone // enum: String
    var deviceSide: DeviceSide = .notApplicable // enum: String
    var deviceNumber: Int16 = 1
    var devicePosition: DevicePosition = .waist  // enum: String
}

CoreDataStack在这里:
final class CoreDataStack {
    static let shared = CoreDataStack()
    private init() {}

    var errorHandler: (Error) -> Void = { error in
            log.error("\(error), \(error._userInfo)")
    }

    private struct constants {
            static let persistentStoreName = "Model"
    }

    private lazy var persistentContainer: NSPersistentContainer = {
            let container = NSPersistentContainer(name: constants.persistentStoreName)
            container.loadPersistentStores(completionHandler: { [weak self] (storeDescription, error) in
                    if let error = error {
                            self?.errorHandler(error)
                    }
            })
            return container
    }()

    lazy var viewContext: NSManagedObjectContext = {
            self.persistentContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
            self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
            try! self.persistentContainer.viewContext.setQueryGenerationFrom(.current)
            return self.persistentContainer.viewContext
    }()

    private lazy var backgroundContext: NSManagedObjectContext = {
            let context = self.persistentContainer.newBackgroundContext()
            context.mergePolicy = NSMergePolicy.mergeByPropertyStoreTrump
            return context
    }()

    func performForegroundTask(_ block: @escaping (NSManagedObjectContext) -> Void) {
            self.viewContext.performAndWait {
                    block(self.viewContext)
            }
    }

    func performBackgroundTask(_ block: @escaping (NSManagedObjectContext) -> Void) {
            backgroundContext.perform {
                    block(self.backgroundContext)
            }
    }

    func saveBackgroundContext() {
            viewContext.performAndWait {
                    do {
                            if self.viewContext.hasChanges {
                                    try self.viewContext.save()
                            }
                    } catch {
                            self.errorHandler(error)
                    }

                    self.backgroundContext.perform {
                            do {
                                    if self.backgroundContext.hasChanges {
                                            try self.backgroundContext.save()
                                            self.backgroundContext.refreshAllObjects()
                                    }
                            } catch {
                                    self.errorHandler(error)
                            }
                    }
            }
    }

    func saveViewContext() {
            viewContext.perform {
                    if self.viewContext.hasChanges {
                            do {
                                    try self.viewContext.save()
                            } catch {
                                    self.errorHandler(error)
                            }
                    }
            }
    }

    func saveViewContextAndWait() {
            viewContext.performAndWait {
                    if self.viewContext.hasChanges {
                            do {
                                    try self.viewContext.save()
                            } catch {
                                    self.errorHandler(error)
                            }
                    }
            }
    }
}

代码在startRecording方法的以下行中轰炸:
try context.obtainPermanentIDs(for: [sessionInfo])

编辑:

我创建了一个简化的测试应用程序,该应用程序仅由CoreDataStack和一个模型组成,该模型具有一个实体,其中一个实体的类型为string。我仍然只在3x iPhone 5上遇到相同的错误。 5s,6、6s,7都可以正常工作。

这是否意味着问题可能出在CoreDataStack上?

Github回购here

最佳答案

几个人问我是否解决了这个问题,所以这就是我所做的。这并不是真正的解决方案,而是更多的解决方法。它可能不适合所有人,但对我有用。

我所做的就是删除行

try! self.persistentContainer.viewContext.setQueryGenerationFrom(.current)

CoreDataStack开始,问题就消失了...

关于ios - EXC_BAD_ACCESS代码= EXC_ARM_DA_ALIGN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41521642/

10-10 17:03