サーバーからメッセージを取得する既存のコードがあります。サーバーはDjangoにあり、改ページされたデータを返します。したがって、サーバーに行く最初の呼び出しは10の最新の結果をフェッチします。次の呼び出しでより多くの結果が得られるはずです...RxSwiftを使用してAlamofireリクエストを通じて次のページデータを取得します。
ただし、最初の要求がサーバーに送信されているだけです。したがって、最新の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
変数は現在は常にnilです。ここで
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
}
}
}