如何检查记录是否在2个不同的表上?

时间:2015-01-12 19:37:48

标签: swift parse-platform

我正在使用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)
 }
}

1 个答案:

答案 0 :(得分:1)

警告:请原谅我所犯的任何Swift错误。我仍然只是在Objective-C中写作,我已经尽力写好Swift,但我还不是语法方面的专家。

我能提出的最佳答案是,您需要2个查询(这假设您关注Sales[PFUser currentUser]的{​​{1}}:

  1. 获取当前用户的PFUser.currentUser()个对象列表
  2. 获取所有(当前)Sales个对象的列表。
  3. 查询1

    Swift版本

    Offer

    Objective-C Version

    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...
        }
    }
    

    查询2

    Swift版本

    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];
    
        }
    }
     ];
    

    Objective-C Version

    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",当前用户的销售数组,"提供",所有当前优惠的数组

    Swift版本

    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];
    
        }
    
    }
     ];
    

    Objective-C Version

    // 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 ,除非这是您想要的