使用 RxSwift 通过 Alamofire 请求检索下一页数据



我有一些现有的代码可以从服务器获取消息。服务器在 Django 中并返回分页数据。因此,进入服务器的第一个调用获取 10 个最新结果,下一个调用应该带来更多结果,依此类推......

但是,问题是只有第一个请求被发送到服务器。因此,它仅加载 10 个最新结果,没有其他内容。我想修改代码,以便应用程序能够获取所有结果。

杰伦响应

请求:http://192.168.0.127:8000/v1/topic/1/message/

{
  "count": 12,
  "next": "http://192.168.0.127:8000/v1/topic/1/message/?page=2",
  "previous": null,
  "results": [
    {
      "id": 12,
      "topic": 1,
      "photo_url": "",
      "message": "Test12",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:50:42.335694Z"
    },
    {
      "id": 11,
      "topic": 1,
      "photo_url": "",
      "message": "Test11",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:50:13.931648Z"
    },
    {
      "id": 10,
      "topic": 1,
      "photo_url": "",
      "message": "Test10",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:50:07.819994Z"
    },
    {
      "id": 9,
      "topic": 1,
      "photo_url": "",
      "message": "Test9",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:49:59.896686Z"
    },
    {
      "id": 8,
      "topic": 1,
      "photo_url": "",
      "message": "Test8",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:47.268219Z"
    },
    {
      "id": 7,
      "topic": 1,
      "photo_url": "",
      "message": "Test7",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:36.587173Z"
    },
    {
      "id": 6,
      "topic": 1,
      "photo_url": "",
      "message": "Test6",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:29.111922Z"
    },
    {
      "id": 5,
      "topic": 1,
      "photo_url": "",
      "message": "Test5",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:22.983123Z"
    },
    {
      "id": 4,
      "topic": 1,
      "photo_url": "",
      "message": "Test4",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:12.986871Z"
    },
    {
      "id": 3,
      "topic": 1,
      "photo_url": "",
      "message": "Test3",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:48:05.376769Z"
    }
  ]
}

请求:http://192.168.0.127:8000/v1/topic/1/message/?page=2

{
  "count": 12,
  "next": null,
  "previous": "http://192.168.0.127:8000/v1/topic/1/message/",
  "results": [
    {
      "id": 2,
      "topic": 1,
      "photo_url": "",
      "message": "Test2",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:47:59.367765Z"
    },
    {
      "id": 1,
      "topic": 1,
      "photo_url": "",
      "message": "Test1",
      "user": {
        "id": 2,
        "first_name": "John",
        "photo": "https://graph.facebook.com/437334666655912/picture/?type=large",
        "last_seen_event": null,
        "blocked": false
      },
      "created_on": "2017-05-22T07:47:49.372195Z"
    }
  ]
}

法典

fileprivate lazy var _getMessagesAction: Action<EnumerableType, EnumerableType> = Action { (last: EnumerableType?) -> Observable<EnumerableType> in
        self.isNewRequest = last?.next == nil ? true : false
        return self.messagesRequest(last)
}

last变量现在始终为零。

typealias EnumerableType = Enumerable<Message>

这里是可枚举

public struct Enumerable<EnumerableType: ResponseCollectionModelSerializable> : ResponseModelSerializable {
    let count: Int
    let prev: String?
    let next: String?
    let results: [EnumerableType]?
    public init?(response: HTTPURLResponse, json: JSON) {
        guard let countOBj = json["count"] else {
            return nil
        }
        self.count = countOBj as! Int
        self.prev = json["previous"] as? String
        self.next = json["next"] as? String
        if let results = json["results"] as? AnyObject {
            self.results = EnumerableType.collection(response: response, json: JSON(results))
        } else {
            self.results = nil
        }
    }
}

嗯...我觉得您发布的帖子不足以为您的情况创建特定示例,但是有用于执行此类操作的示例代码。

在 RxSwift GitHub 存储库中,有几个示例,包括以页面形式加载数据的 GitHubSearchRepository。

我还写了这个要点:https://gist.github.com/danielt1263/10bc5eb821c752ad45f281c6f4e3034b 其中包括测试并详细介绍了视图控制器的视图模型,以及用于显示活动指示器的可观察量。

最新更新