我知道在枚举数组时不能添加或删除数组中的元素,但是...我不知道!我认为我已经缩小了崩溃发生的位置,但是对我来说没有任何意义。
func sendToCloud() {
for quoteNumber in newQuotes {
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Quotes")
let predicate = NSPredicate(format: "quoteNumber == %@", quoteNumber)
fetchRequest.predicate = predicate
fetchRequest.fetchLimit = 1
do {
//The next two lines is where the crash happens...
quotes = try context.fetch(fetchRequest)
let currentQuote = quotes.first as! Quotes
//-------------------------------------------
prepRecordForCoreData(obj: currentQuote, recordName: currentQuote.ckrecordname!, recordType: "Quotes")
repRecordForCoreData(obj: currentQuote.customer!, recordName: currentQuote.ckrefcustomer!, recordType: "Customers")
} catch let error as NSError {
print("ERROR SAVING TO CLOUD", error)
}
}
//clear array
newQuotes = []
coreData.saveContext { (success) -> Void in
if success {
AppData.uploadRecords()
}
}
}
以防万一...
func prepRecordForCoreData(obj: SignatureManagedObject, recordName: String, recordType: String) {
let zone = CKRecordZone(zoneName: Contstants.Strings.zoneName)
let id = CKRecord.ID(recordName: recordName, zoneID: zone.zoneID)
let record = CKRecord(recordType: recordType, recordID: id)
let coder = NSKeyedArchiver(requiringSecureCoding: true)
record.encodeSystemFields(with: coder)
let metadata = coder.encodedData
obj.ckmetadata = metadata
obj.ckrecordname = recordName
obj.ckupload = true
}