ios - 喜欢通过客户端使用两个 API 作为 IOS 应用程序(Swift)的功能实现

标签 ios swift tastypie

我们正在尝试为我们的 IOS 应用构建一个简单的跟随功能。我们有两个 API,我们的 Brand API 有一个对象数组,每个对象中包含我们品牌的唯一品牌 ID。以及我们存储用户的 Firebase API。在 Firebase API 中,它有一个名为 followingBrands 的键和一个对象数组,该对象数组由 来 self 们的Brand API` 的唯一品牌 ID 作为键,值为 true。一旦用户在我们的应用程序上喜欢该品牌并关注了该品牌,就会创建这些对象。

当应用加载时,我们会检查 Firebase API 的品牌 ID 键是否与 Brand API 的品牌 ID 匹配,然后显示星号以表明用户是已经关注该品牌。

我们的问题是 Brand API 是通过分页(即偏移量)实现的,所以如果不是所有唯一的品牌 ID 都可用于与我们的比较,我们将如何验证他们关注的所有品牌 Firebase API ?

我们在IOS端使用swift。 Brand API 是使用 django-tastypie 构建的

Firebase API

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

品牌 API

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

最佳答案

你可以做一个技巧,只要有一个 brandID 等于你的 fireBase followingBrands 键就显示星星。你不需要在 brandAPI 中检查它,因为我认为你已经在 fireBaseAPI 中插入了每个品牌,然后是用户,所以这对你有用。

 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")
}

关于ios - 喜欢通过客户端使用两个 API 作为 IOS 应用程序(Swift)的功能实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058110/

相关文章:

ios - iphone接到电话时如何获取号码

ios - 如何在 Swift 中显示已解析的 JSON 数据?

iOS 应用程序运行断点

django - 没有默认行为的限制

django - 使用外键创建 tastypie 资源而不将外键公开为资源

ios - React Native iOS Sentry - 阻止调用的参数过多,预​​期为 1,实际有 3

swift - 收到错误无法在属性初始值设定项中使用实例成员 'url';属性初始值设定项在 'self' 可用之前运行

objective-c - 在 swift 中子类化 'PDFView' - 找不到接口(interface)声明

ios - 如何在少数情况下包含额外属性的情况下创建枚举

python - 如何筛选相关对象中的字段?