3

I'm getting a JSON response of the format:

{
    "current_page":1,
    "data":[
        {
            "id":1,
            "title":"Title 1"
        },
        {
            "id":2,
            "title":"Title 2"
        },
        {
            "id":3,
            "title":"Title 3"
        }
    ]
}

As you can see, data contains a list of objects, in this case, a list of Posts. Here is my Realm/Objectmapper Post class:

import RealmSwift
import ObjectMapper

class Post: Object, Mappable {
    let id = RealmOptional<Int>()
    @objc dynamic var title: String? = nil

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {

    }
}

I created a generic class (I'm not sure it's written right) to handle Pagination responses. I want it to be generic because I have other pagination responses that return Users instead of Posts, among other objects.

Here is my current Pagination class:

import ObjectMapper

class Pagination<T: Mappable>: Mappable {
    var data: [T]?

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        data <- map["data"]
    }
}

However, I'm not sure if I've written this class right.

And here is the class where I call the endpoint that sends back the pagination data (I've removed irrelevant code):

var posts = [Post]()

provider.request(.getPosts(page: 1)) { result in
    switch result {
    case let .success(response):
        do {
            let json = try JSONSerialization.jsonObject(with: response.data, options: .allowFragments)

            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            // Not sure what to do here to handle and retrieve the list of Posts
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            // Eventually, I need to append the posts to the variable
            // self.posts.append(pagination.data)

            // Reload the table view's data
            self.tableView.reloadData()
        } catch {
            print(error)
        }
    case let .failure(error):
        print(error)
        break
    }
}

How do I handle the JSON response correctly in order to get the list of Posts and then append them to the var posts = [Post]() variable? Do I need to make any changes to my Pagination class?

user6724161
  • 195
  • 2
  • 15
  • I assume this question can not be answered without knowing about ObjectMapper (and perhaps also Realm)? – Joakim Danielson Feb 17 '19 at 18:16
  • I'm not sure, I think it should be possible to answer without knowing Realm or Objectmapper. – user6724161 Feb 17 '19 at 19:35
  • I've recently created an app which used an API that had pagination. If you know the total number of pages, I suggest you take a look at "UITableViewDataSourcePrefetching". It is specially designed for tableView/collectionView which use pagination. This way you won't need to append manually to your array of data and track on which page you're right now, and so on. Ray Wenderlich has a great example on this. – Starsky Feb 17 '19 at 20:47

1 Answers1

0

Once you have your json, it is easy to parse it using object mapper:

let pagination = Mapper<Pagination<Post>>().map(JSONObject: json)

It could be further generalized, I have used a direct reference as an example. Your Pagination class can also hold the current page index value.

I think you are also missing the implementation of the mapping(map:) function in your Post class, it should be something like this:

func mapping(map: Map) {
    title <- map["title"]
}
pckill
  • 3,709
  • 36
  • 48