В моей функции saveOrder()
(которая вызывается наблюдателем Firebase) я создаю новую запись сущности Order
и устанавливаю цикл for in
.В цикле я вызываю decrementInventory()
, который изменяет запись сущности Product
(нет связи между ними), и я создаю новый Item
дочерний элемент Order
в coreData
.Моя проблема: в то время как decrementInventory()
вызывается один раз и выполняет свою работу правильно, я получаю дважды Item
созданных записей.Цикл основан на productIdListArray.count
, но для count
из 1 он повторяется дважды и т. Д. Вы можете определить, почему цикл повторяется дважды?Как всегда большое спасибо.
Это функция, она немного длинная, как для iOS9, так и для 10:
static func saveOrder(completed: @escaping(Bool) ->(),state: String ,orderId: String, orderDate: String, customerId: String, customerName: String, customerFcmToken: String, orderPrice: String, itemsIdList: String, itemsList: String, itemsCategoryList: String, itemsPriceList: String, promotionList: String) throws {
print("Order.saveOrder() : STARTED")
let context = CoreData.databaseContext
let userRequest: NSFetchRequest<User> = User.fetchRequest()
do {
let userFetch = try context.fetch(userRequest)
print("Order.saveOrder() : fetching user")
for userValue in userFetch {
if userValue.name == UserDetails.fullName {
print("Order.saveOrder() : User is: \(userValue.name!)")
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 {
print("Order.saveOrder() : 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.customerId = customerId
order.customerFcmToken = customerFcmToken
order.orderPrice = orderPrice
order.itemsIdList = itemsIdList
order.itemsList = itemsList
order.itemsCategoryList = itemsCategoryList
order.itemsPriceList = itemsPriceList
order.promotionList = promotionList
userValue.addToOrders(order)
print("Order.saveOrder() : Order is: \(order)")
let actions: [UNNotificationAction] = [UNNotificationAction(identifier: "chiudi", title: "Chiudi", options: [.foreground])]
LocalNotifications.newTimeIntervalNotification(notificationType: "New order", actions: actions, categoyIdentifier: "New order", title: "Ordine", body: "Hai un nuovo ordine", userInfo: [:] , timeInterval: 0.1, repeats: false)
var productIdListArray:[String] = itemsIdList.components(separatedBy: ",")
var productNameListArray:[String] = itemsList.components(separatedBy: ",")
var productCategoryListArray: [String] = itemsCategoryList.components(separatedBy: ",")
var productPriceListArray: [String] = itemsPriceList.components(separatedBy: ",")
print("productIdListArray.count is : \(productIdListArray.count)")
var productPromotionListArray: [String] = promotionList.components(separatedBy: ",")
for product in 0..<productIdListArray.count {
print("productIdListArray.count is : \(productIdListArray.count)")
do {
try Product.decrementIventory(completed: { (true) in
let item = Item(context: context)
item.order?.orderId = orderId
item.itemId = productIdListArray[product]
item.itemName = productNameListArray[product]
item.price = productPriceListArray[product]
item.category = productCategoryListArray[product]
item.promotion = productPromotionListArray[product]
let fullDate = Conversions.dateConvert(dateString: order.orderDate!)! as NSDate
print("Order.saveOrder() : fullDate is: \(fullDate)")
item.date = fullDate
order.addToItems(item)
print("Order.saveOrder() : New item record is: \(item)")
print("Order.saveOrder: Inventory seccessfully updated for product: \(productNameListArray[product])")
}, productId: productIdListArray[product])
} catch {
print("Order.saveOrder() : 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.customerId = customerId
order.customerName = customerName
order.customerFcmToken = customerFcmToken
order.orderPrice = orderPrice
order.itemsIdList = itemsIdList
order.itemsList = itemsList
order.itemsCategoryList = itemsCategoryList
order.itemsPriceList = itemsPriceList
order.promotionList = promotionList
userValue.addToOrders(order)
LocalNotifications.newTimeIntervalNotification(notificationType: "New order", actions: [], categoyIdentifier: "New order", title: "Ordine", body: "Hai un nuovo ordine", userInfo: [:], timeInterval: 0.1, repeats: false)
var productIdListArray:[String] = itemsIdList.components(separatedBy: ",")
var productNameListArray:[String] = itemsList.components(separatedBy: ",")
var productCategoryListArray: [String] = itemsCategoryList.components(separatedBy: ",")
var productPriceListArray: [String] = itemsPriceList.components(separatedBy: ",")
var productPromotionListArray: [String] = promotionList.components(separatedBy: ",")
for product in 0..<productIdListArray.count {
do {
try Product.decrementIventory(completed: { (true) in
// let entityDescription = NSEntityDescription.entity(forEntityName: "Item", in: context)
let item = Item(entity: entityDescription!, insertInto: context)
// item.order?.user?.name = userValue.name!
item.order?.orderId = orderId
item.itemId = productIdListArray[product]
item.itemName = productNameListArray[product]
item.price = productPriceListArray[product]
item.category = productCategoryListArray[product]
item.promotion = productPromotionListArray[product]
let fullDate = Conversions.dateConvert(dateString: order.orderDate!)! as NSDate
print("Order.saveOrder() : fullDate is: \(fullDate)")
item.date = fullDate
order.addToItems(item)
print("Order.saveOrder() : New item record is: \(item)")
print("Order.saveOrder: Inventory seccessfully updated for product: \(productNameListArray[product])")
}, productId: productIdListArray[product])
} catch {
print("Order.saveOrder() : Error in decrementing inventory : \(error)")
}
}
} // end of iOS 9
} else {
print("Order.saveOrder() : Order is already saved")
return
}
} catch {
print("Order.saveOrder():Error in fetching orders: \(error)")
}
}
}
} catch {
print("Order.saveOrder() : Error in fetching user: \(error)")
}
do {
try context.save()
print("Order.saveOrder(): New order is saved do CoreData")
completed(true)
} catch {
print(" Order.saveOrder(): Error saving new order to CoreData: \(error)")
}
print("Order.saveOrder() : ENDED")
}
, и это обозреватель Firebase:
static func getOrders(completed: @escaping (Bool) -> ()) {
print("getOrders() : started")
let ref = Database.database().reference()
// .childAdded
ref.child("Continent").child("Europe").child("Country").child("\(UserDetails.country!)").child("Region").child(UserDetails.region!).child("City").child(UserDetails.city!).child("Shops").child(UserDetails.fullName!).child("Orders").observe(.childAdded) { (snapshot) in
print("snapshot is: \(snapshot)")
guard let value = snapshot.value as? [String : String] else {return}
let orderId = value["Order Id"]!
let orderDate = value["Order Date"]!
let customerId = value["User Id"]!
let customerName = value["User Name"]!
let customerFcmToken = value["User fcmToken"]!
let orderPrice = value["Order Price"]!
let itemsIdList = value["Items Id List"]!
let itemsList = value["Items List"]!
let itemsCategoryList = value["Items Category List"]!
let itemsPriceList = value["Items Price List"]!
let itemsPromotionList = value["Items Promotion List"]!
do {
try Order.saveOrder(completed: { (true) in
print("getOrders(): order saved to CoreData")
// send push to customer
PushNotifications.sendPushNotification(to: customerFcmToken, title: "Order number: \(String(describing: orderId))", subtitle: " Shop: \(String(describing: UserDetails.fullName!))", body: "Thank you \(customerName)! We received your order and we'll let you know when we start preparing it and when it's ready. Bye ")
// localize push
// PushNotifications.sendPushNotification(to: customerFcmToken, title: NSLocalizedString(String(format: "Order number: %1@", orderId), comment: ""), subtitle: NSLocalizedString(String(format: "Shop: %1@", UserDetails.fullName!), comment: ""), body: String(format: "Thank you %1@! We received your order and we'll let you know when we start preparing it and when it's ready. Bye ", customerName))
}, state: "received", orderId: orderId, orderDate: orderDate, customerId: customerId, customerName: customerName, customerFcmToken: customerFcmToken, orderPrice: orderPrice,itemsIdList: itemsIdList, itemsList: itemsList, itemsCategoryList: itemsCategoryList, itemsPriceList: itemsPriceList, promotionList: itemsPromotionList)
print("getOrders() : ended, now observing")
completed(true)
} catch {
print("getOrders(): Error in saving snapshot to Core Data : \(error)")
}
}
}