在我的应用中,用户可以出售商品。买方查询其区域(例如1英里半径),并获得该区域内的所有卖方,并出现卖方要出售的所有帖子/物品。
一旦买家看到某商品,他们可以单击该单元格,将显示一个新的vc,他们将看到价格和说明,从那里他们可以将该商品添加到购物车中,其中另一个vc将被显示然后从那里转到另一个vc进行付款。
HomeVC > Price/DescriptionVC > ShoppingCartVC > PaymentVC
HomeVC是最初从数据库中获取所有数据的位置。返回数据后,可以有多个卖家,每个卖家都有多个要出售的帖子/物品(全部显示)。
如果买方使用Price / DescriptionVC(或以下任何一个vcs),而卖方之一更改了其用户名或所查看商品的价格,如何在数据库中监视该信息更新vc以反映信息中的更改?
例如,买方在Price / DescriptionVC上,正在由叫Peter的卖方以10美元的价格看这双冰鞋。彼得将名字改成保罗,并将冰鞋的价格提高到20美元。我认为现在应该在Price / DescriptionVC中更新这些更改,以便买方将新的20美元价格支付给一个名叫Paul的人,而不是10美元的价格支付给一个名叫Peter的人。
以下代码:
代码:
var arrOfSellerIds = [String]()
var arrOfSellers = [Seller]()
var tableData = [Post]()
// #1 get all the sellers within a 1 mile radius
regionQuery = geoFire.query(with: oneMileRegion) // set earlier
queryHandle = regionQuery?.observe(.keyEntered, with: {(key: String!, location: CLLocation!) in
self.arrOfSellerIds.append(key)
})
regionQuery?.observeReady({
self.fetchTheSellersFromSellersRef()
})
// #2 get the seller's uid, name, profile pic, and email
func fetchTheSellersFromSellersRef() {
let dispatchGroup = DispatchGroup()
for sellerId in arrOfSellerIds {
dispatchGroup.enter()
let sellersRef = Database.database().reference().child("sellers").child(sellerId)
sellersRef.observeSingleEvent(of: .value, with: {(snapshot) in
guard let dict = snapshot.value as? [String: Any] else { dispatchGroup.leave(); return }
let seller = Seller(dict: dict) // userId, username, profileURL, email
self.arrOfSellers.append(seller)
dispatchGroup.leave()
})
dispatchGroup.notify(queue: .global(qos: .background)) {
self.getAllPostsFromSellerAndReloadCollectionView()
}
}
}
// #3 get **all** the posts from each seller and append them to the tableData
func getAllPostsFromSellerAndReloadCollectionView() {
let dispatchGroup = DispatchGroup()
for seller in arrOfSellers {
dispatchGroup.enter()
let postsRef = Database.database().reference().child("posts").child(seller.sellerId!)
postsRef.observeSingleEvent(of: .value, with: {(snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { dispatchGroup.leave(); return }
for (key, value) in dictionaries {
guard let dict = value as? [String: Any] else { continue }
let post = Post(seller: seller, dict: dict)
self?.tableData.append(post)
}
dispatchGroup.leave()
})
dispatchGroup.notify(queue: .global(qos: .background)) { [weak self] in
// sort the tableData by postDate and reload collectionView on mainQueue
}
}
}
卖方和邮政模型
class Seller {
var sellerId: String?
var username: String?
var profileURl: String?
var email: String?
}
class Post {
var postId: String?
var postDate: Double?
var seller: Seller?
var item: String?
var price: Double?
var description: String?
}
仅供参考,我知道我可以对tableData进行排序并根据postId和/或SellerId更新信息,但是我仍然看不到何时会收到通知,通知我卖家或他们的帖子发生了变化是在数据库中制作的。我将这两个函数添加到哪里?
func changeToSellerOccurredTo(_ sellerId: String) {
for post in tableData {
if post.seller!.sellerId! == sellerId {
if let indexOfItem = tableData.index(where: { $0.seller.sellerId ?? "" == sellerId }) {
let post = tableData[indexOfItem]
// update this post here in HomeVC and send a Notification to the Price/DescriptionVC, ShoppingCartVC, and PaymentVC to update the change to the post's seller's property
}
}
}
}
func changeToPostOccurredTo(_ postId: String) {
for post in tableData {
if post.postId! == postId {
if let indexOfItem = tableData.index(where: { $0.postId ?? "" == postId }) {
let post = tableData[indexOfItem]
// update this post here in HomeVC and send a Notification to the Price/DescriptionVC, ShoppingCartVC, and PaymentVC to update the change to the post itself
}
}
}
}