我正在使用Parse.com并寻找处理此问题的有效方法。
我有一个Sales
表,其中包含User
引用列和Offer
引用列。
销售表跟踪哪些用户进入要约(即10%折扣)。
所以我有以下子类
Offers
Users
Sales
我想显示所有可用的优惠,但设置了一个本地的hasBeenUsed标志,以便我可以以不同的方式显示优惠(如果已经使用过)。
目前我可以创建一个可用优惠的本地数组,但我不确定如何相应地设置这个hasBeenUsed标志。
var query = Message.query()
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
for object in objects {
var offer = Offer()
offer.info = object["info"] as String
if (also on the Sales list)
offer.hasBeenUsed = true
}
self.offers.insert(offer, atIndex: 0)
}
}
答案 0 :(得分:1)
警告:请原谅我所犯的任何Swift错误。我仍然只是在Objective-C中写作,我已经尽力写好Swift,但我还不是语法方面的专家。
我能提出的最佳答案是,您需要2个查询(这假设您关注Sales
或[PFUser currentUser]
的{{1}}:
PFUser.currentUser()
个对象列表Sales
个对象的列表。Offer
var query = PFQuery(className:"Sales")
query.includeKey("Offer")
query.whereKey("User", equalTo:PFUser.currentUser())
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Assign the objects to a property or something
self.sales = objects
// **Then**, run query 2
runQuery2()
} else {
// Do error handling...
}
}
PFQuery *query = [PFQuery queryWithClassName:@"Sales"];
[query includeKey:@"Offer"];
[query whereKey:@"User" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error != nil) {
// Do error handling
} else {
// Assign the objects to a property or something...
self.sales = objects;
// **Then**, run query 2
[self runQuery2];
}
}
];
var query = PFQuery(className:"Offers")
// Whatever query constraints you need to get "current" offers
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Assign the objects to a property or something
self.offers = objects;
// At this point, you have all the data you need, so call the "filter" method
filter()
} else {
// Do error handling...
}
}
这假设有2个属性:" sales",当前用户的销售数组,"提供",所有当前优惠的数组 强>
PFQuery *query = [PFQuery queryWithClassName:@"Offers"];
// Whatever query constraints you need to get "current" offers
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error != nil) {
// Do error handling
} else {
// Assign the objects to a property or something
self.offers = objects;
// At this point, you have all the data you need, so call the "filter" method
[self filter];
}
}
];
// First, get all of the "redeemed" offers into an array
var localOffers = [PFObject]()
for sale in self.sales {
localOffers.append(sale.offer)
// It might be easier to do it this way
// localOffers.append(sale.offer.objectId)
}
// Next, iterate the "all offers" array and mark each offer as redeemed/not redeemed
for offer in self.offers {
if (localOffers.containsObject(offer)) {
// Alternative check
// if (localOffers.containsObject(offer.objectId)) {
offer.hasBeenUsed = true
} else {
offer.hasBeenUser = false
}
}
此时,根据要约是否已兑现,您已获得显示数据所需的内容。请注意不要将// First, get all of the "redeemed" offers into an array
NSMutableArray *localOffers = [NSMutableArray array];
for (PFObject *sale in self.sales) {
[localOffers addObject:sale.offer];
// It might be easier to do it this way
// [localOffers addObject:self.offer.objectId];
}
// Next, iterate the "all offers" array and mark each offer as redeemed/not redeemed
for (PFObject *offer in self.offers) {
if ([localOffers containsObject:offer]) {
// Alternative check
// if ([localOffers containsObject:offer.objectId]) {
offer[@"hasBeenUsed"] = @(YES);
} else {
offer[@"hasBeenUsed"] = @(NO);
}
}
值保存到Parse ,除非这是您想要的。