如何检查一条记录是否在 2 个不同的表上?
How to check if a record is on 2 different tables?
我正在使用 Parse.com 并寻找一种有效的方法来处理这个问题。
我有一个 Sales
table,它有一个 User
参考列和一个 Offer
参考列。
销售 table 跟踪哪些用户前来领取优惠(即 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)
}
}
警告:请原谅我犯的任何 Swift 错误。我还在写Objective-C,我已经尽力写好Swift,但我还不是语法专家
我能想到的最佳答案是您需要 2 个查询(假设您关心 Sales
用于 [PFUser currentUser]
或 PFUser.currentUser()
):
- 获取当前用户的
Sales
个对象列表
- 获取所有(当前)
Offer
个对象的列表。
查询 1
Swift版本
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...
}
}
Objective-C版本
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];
}
}
];
查询 2
Swift版本
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...
}
}
Objective-C版本
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];
}
}
];
过滤方法
这假定了 2 个属性:"sales",当前用户的销售数组,以及 "offers",所有当前优惠的数组
Swift版本
// 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
}
}
Objective-C版本
// 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);
}
}
至此,您已获得根据优惠是否已兑换以不同方式显示数据所需的内容。请注意不要将 hasBeenUsed
值保存到 Parse ,除非那是你想要的。
我正在使用 Parse.com 并寻找一种有效的方法来处理这个问题。
我有一个 Sales
table,它有一个 User
参考列和一个 Offer
参考列。
销售 table 跟踪哪些用户前来领取优惠(即 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)
}
}
警告:请原谅我犯的任何 Swift 错误。我还在写Objective-C,我已经尽力写好Swift,但我还不是语法专家
我能想到的最佳答案是您需要 2 个查询(假设您关心 Sales
用于 [PFUser currentUser]
或 PFUser.currentUser()
):
- 获取当前用户的
Sales
个对象列表 - 获取所有(当前)
Offer
个对象的列表。
查询 1
Swift版本
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...
}
}
Objective-C版本
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];
}
}
];
查询 2
Swift版本
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...
}
}
Objective-C版本
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];
}
}
];
过滤方法
这假定了 2 个属性:"sales",当前用户的销售数组,以及 "offers",所有当前优惠的数组
Swift版本
// 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
}
}
Objective-C版本
// 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);
}
}
至此,您已获得根据优惠是否已兑换以不同方式显示数据所需的内容。请注意不要将 hasBeenUsed
值保存到 Parse ,除非那是你想要的。