gpt4 book ai didi

ios - for in 循环循环两次 Swift

转载 作者:行者123 更新时间:2023-11-28 13:35:24 25 4
gpt4 key购买 nike

在我的 saveOrder() 函数(由 Firebase 观察者调用)中,我创建了一个新的 Order 实体条目并设置了一个 for in 循环。在循环中,我调用 decrementInventory() 修改 Product(它们之间没有关系)实体条目,并创建一个新的 Item 实体子项coreData 中的Order。我的问题是:当 decrementInventory() 被调用一次并正确完成工作时,我创建了两次 Item 记录。该循环基于 productIdListArray.count ,但是对于 1 的 count 循环两次等等。您能看出为什么要循环两次吗?一如既往地非常感谢。

这是函数,对于 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)")
}
}
}

最佳答案

经过相当多的尝试,我发现我错误地调用了两次 decrementInventory() 的完成,这实际上是 for in 循环的开始保存订单()。希望这会帮助其他人。干杯

关于ios - for in 循环循环两次 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com