I have a function saveOrders()
that checks if snapshot
from firebase corresponds to a record already been saved to CoreData by comparing orderId
of the saved orders against the one that get passed from snapshot
in function's closure. If it matches it returns, otherwise in the else
statement creates a new record to CoreData
depending on iOS version. Weirdly enough it worked yesterday, but today it jumps directly the else
statement when ids don't match. Today I only added the commented out part. Can you spot where it got broken? Here's the code:
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)")
}
}
Aucun commentaire:
Enregistrer un commentaire