Search code examples
iosswifttastypie

Liking feature implementation using two API's via client side as IOS app (Swift)


We are trying to build a simple follow feature for our IOS app. We have two API's our Brand API with an array of objects containing unique brand ids for our brands within each object. And our Firebase API where we store users. Within the Firebase API it has a key called followingBrands with and array of objects called composed of the unique brand ids from ourBrand API` as keys with the value true. The objects are created once a user has followed the brand from liking the brand on our app.

When the app loads we check to see if Firebase API's brand ids keys matches the Brand API's brand ID then show a star to indicate the user is already following the brand.

Our problem is Brand API is implemented with pagination, (i.e. offset), so how will we verify all brands they are following if not all the unique brand ids will be available to compare with our Firebase API ?

We use swift on the IOS side. And the Brand API is built using django-tastypie

Firebase API

"user_id" : {
  "currentFollowingCount" : 0,
  "displayName" : "",
  "email" : "",
  "followingBrands" : {
    "unique_brand_id" : true
  },
  "provider" : "Facebook",
  "userID" : "user_id"
}

Brand API

{
  "labels": [
    {
      "id": "unique_brand_id"
    }
  ],
  "meta": {
    "limit": 10,
    "next": "/api/?limit=10&offset=10",
    "offset": 0,
    "previous": null,
    "total_count": 33
  }
}

Solution

  • you can do a trick just show the star whenever there is a brandID equals to you fireBase followingBrands key. you don't need to check it in brandAPI as i think you already insert every brand followed by user in the fireBaseAPi so this will work for you.

     let fireBaseResponse:[String:AnyObject?] = [
                            "followingBrands":["unique_brand_id":true]
                            ]
    
    let customObject = ["customObjectBrandID":"unique_brand_id"]//yourObject
    
    let followingBrandsArr = (fireBaseResponse["followingBrands"] as! [String:Bool]).keys
    
    //now do a check for Star
    let myObjectBrandID = customObject["customObjectBrandID"]
    if followingBrandsArr.contains(myObjectBrandID!) == true{
        print("user is following this brand so show the follow star")
    }else{
        print("don't show or hide it")
    }