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

2

6

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
  }
}
Wolffish answered 20/8, 2016 at 19:49 Comment(0)
B
1

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")
}
Benford answered 23/8, 2016 at 9:50 Comment(0)
P
1

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.

Phyllotaxis answered 23/8, 2016 at 8:55 Comment(0)
B
1

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")
}
Benford answered 23/8, 2016 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.