У меня есть функция saveOrders()
, которая проверяет, соответствует ли snapshot
из firebase записи, уже сохраненной в CoreData, путем сравнения orderId
сохраненных ордеров с тем, который был передан из snapshot
при закрытии функции.Если он совпадает, он возвращается, в противном случае в операторе else
создается новая запись для CoreData
в зависимости от версии iOS.Как ни странно, вчера он работал, но сегодня он переходит непосредственно в оператор else
, когда идентификаторы не совпадают.Сегодня я только добавил закомментированную часть.Можете ли вы определить, где он сломался?Вот код:
static func saveOrder(orderId: String, orderDate: String, customerName: String, orderPrice: String, itemsIdList: String, itemsList: String) throws {
let context = CoreData.databaseContext
let userRequest: NSFetchRequest<User> = User.fetchRequest()
do {
let userFetch = try context.fetch(userRequest)
print("@@@@@@@@@@@@@@@@@@ fetching user")
for userValue in userFetch {
if userValue.name == UserDetails.fullName {
print("User is: \(userValue.name!)") //correct
let orderRequest: NSFetchRequest<Order> = Order.fetchRequest()
let predicate = NSPredicate(format: "orderId == %@", orderId)
orderRequest.predicate = predicate
// orderRequest.fetchLimit = 1
do{
let orderFetch = try context.fetch(orderRequest)
// if orderFetch.count == 0 {
for order in orderFetch {
if order.orderId == orderId {
print("Order is already saved")
return
} else {
print("@@@@@@@@@@@@ order is new")
if #available(iOS 10.0, *) {
let order = Order(context: context)
order.user?.name = userValue.name!
order.orderId = orderId
order.orderDate = orderDate
order.customerName = customerName
order.orderPrice = orderPrice
order.itemsIdList = itemsIdList
order.itemsList = itemsList
userValue.addToOrders(order)
print("Order is: \(order)")
let actions: [UNNotificationAction] = [UNNotificationAction(identifier: "chiudi", title: "Chiudi", options: [.foreground])]
Notifications.newTimeIntervalNotification(notificationType: "New order", actions: actions, categoyIdentifier: "New order", title: "Ordine", body: "Hai un nuovo ordine", userInfo: [:] , timeInterval: 5, repeats: false)
// modify inventory
var productIdListArray:[String] = itemsIdList.components(separatedBy: ",")
var productNameListArray:[String] = itemsList.components(separatedBy: ",")
print("productIdListArray is : \(productIdListArray)")
for product in 0..<productIdListArray.count {
// for product in productIdListArray {
do {
try Product.decrementIventory(completed: { (true) in
// create an item entry in Core Data for each item in current order
// let item = Item(context: context)
// item.order?.user?.name = userValue.name!
// item.itemId = productIdListArray[product]
// item.itemName = productNameListArray[product]
// order.addToItems(item)
print("Inventory seccessfully updated for product: \(productNameListArray[product])")
}, productId: productIdListArray[product])
} catch {print("Error in decrementing inventory : \(error)")
}
}
} else {
// Fallback on earlier versions
let entityDescription = NSEntityDescription.entity(forEntityName: "Order", in: context)
let order = Order(entity: entityDescription!, insertInto: context)
order.user?.name = userValue.name!
order.orderId = orderId
order.orderDate = orderDate
order.customerName = customerName
order.orderPrice = orderPrice
order.itemsIdList = itemsIdList
order.itemsList = itemsList
userValue.addToOrders(order)
Notifications.newTimeIntervalNotification(notificationType: "New order", actions: [], categoyIdentifier: "New order", title: "Ordine", body: "Hai un nuovo ordine", userInfo: [:], timeInterval: 5, repeats: false)
var productIdListArray:[String] = itemsIdList.components(separatedBy: ",")
var productNameListArray:[String] = itemsList.components(separatedBy: ",")
for product in 0..<productIdListArray.count {
do {
try Product.decrementIventory(completed: { (true) in
// create an item entry in Core Data for each item in current order
// let entityDescription = NSEntityDescription.entity(forEntityName: "Item", in: context)
// let item = Item(entity: entityDescription!, insertInto: context)
// item.order?.user?.name = userValue.name!
// item.itemId = productIdListArray[product]
// item.itemName = productNameListArray[product]
// order.addToItems(item)
print("Order.saveOrder: Inventory seccessfully updated for product: \(productNameListArray[product])")
}, productId: productIdListArray[product])
} catch {print("Error in decrementing inventory : \(error)")
}
}
} // en of iOS 9 check
} // end of if order.orderId == orderId {} else {
} // end of for in
} catch {
print("Order.saveOrder():Error in fetching orders: \(error)")
}
}
}
} catch {
print("Error in fetching user: \(error)")
}
do {
try context.save()
print("@@@@@@@@@@@@ Order.saveOrder(): New order is saved do CoreData")
} catch {
print("@@@@@@@@@@@@@@ Order.saveOrder(): Error saving new order to CoreData: \(error)")
}
}