Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  }
}
like image 369
Amechi Avatar asked Aug 20 '16 19:08

Amechi


2 Answers

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")
}
like image 149
Er. Khatri Avatar answered Oct 17 '22 13:10

Er. Khatri


It's not really clear what you're trying to accomplish to be honest. The Brand API doesn't do a lot, or? Or is it translating between internal ID's and unique brand ID's? If so, why?

As a simple problem solution you could turn off pagination, according to your output of tastypie there are only 33 records in your set, so setting a limit either via the Meta class, e.g. max_limit = 1000 or when calling the API resource via ?limit=1000 should return all objects. If they don't change much you should probably apply some caching.

like image 35
yvesonline Avatar answered Oct 17 '22 13:10

yvesonline