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

时间:2017-05-23 12:01:05

标签: pagination alamofire rx-swift

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

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

JSON响应

请求: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
        }

    }

}

1 个答案:

答案 0 :(得分:0)

嗯......我不觉得你发布的内容足以为你的情况创建一个具体的例子,但是有一些示例代码可以用来做这类事情。

在RxSwift GitHub仓库中,有几个例子,包括GitHubSearchRepositories,它们在页面中加载数据。

我还写了这个要点:https://gist.github.com/dtartaglia/10bc5eb821c752ad45f281c6f4e3034b,其中包括测试和详细信息视图控制器的视图模型,其中包含用于显示活动指示符的Observable。