I'm quite new to Swift and programming altogether. I am very keen on learning all the right ways. So any additional tips or remarks are always appreciated.
I'm doing a HTTP request to an api and that works fine. The problem is that it's limited to 100 results per request. Theres's an optional offset and a limit i can set. If i give a limit of 101 i get a server error saying: "Bad Request: Invalid value specified for limit. Maximum allowed value is 100." The total is 101, so i need to do at least two requests. Only after receiving the total data of all the requests i want to populate my tableview. This is what i have:
class Book {
var id: Int
var title: String
let description: String
var coverImage: String
var isbn: String
var publisherID: Int
var publisherName: String
var authorID: Int
var authorFirstName: String
var authorLastName: String
class func getDataFromJson(completionHandler: ([Book]) -> ()) {
var books = [Book]()
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let request = NSURLRequest(URL: NSURL(string: "http://example.website.nl/books/highlighted")!)
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
if let booksFromResult = json["books"] as? [[String: AnyObject]] {
for book in booksFromResult {
let bookID = book["id"] as! Int
let bookTitle = book["title"] as! String
let bookDescription = book["description"] as! String
let bookCoverImage = book["cover_url"] as! String
let bookISBN = book["isbn"] as! String
if let bookPublisher = book["publisher"] as? [String: AnyObject] {
let bookPublisherID = bookPublisher["id"] as! Int
let bookPublisherName = bookPublisher["name"] as! String
if let bookAuthor = book["author"] as? [String: AnyObject] {
let bookAuthorID = bookAuthor["id"] as! Int
let bookAuthorFirstname = bookAuthor["first_name"] as! String
let bookAuthorLastName = bookAuthor["last_name"] as! String
books.append(Book(id: bookID, title: bookTitle, description: bookDescription, coverImage: bookCoverImage, isbn: bookISBN, publisherID: bookPublisherID, publisherName: bookPublisherName, authorID: bookAuthorID, authorFirstName: bookAuthorFirstname, authorLastName: bookAuthorLastName))
}
}
}
print(books.count)
}
dispatch_async(dispatch_get_main_queue(),{
completionHandler(books)
})
} catch {
print("error serializing JSON: \(error)")
}
}
}
task.resume()
}
init(id: Int, title: String, description: String, coverImage: String, isbn: String, publisherID: Int, publisherName: String, authorID: Int, authorFirstName: String, authorLastName: String) {
self.id = id
self.title = title
self.description = description
self.coverImage = coverImage
self.isbn = isbn
self.publisherID = publisherID
self.publisherName = publisherName
self.authorID = authorID
self.authorFirstName = authorFirstName
self.authorLastName = authorLastName
}
}
I have been trying to solve this for more than 24 hours. I have really searched here and on the web for an example. The little i found here couldn't help me.
My thoughts are of how this should be done:
- make first request -> store data somewhere
- make second request -> add data to stored data
- make last request -> add data to stored data
- send data to populate tableview.
Should i use an array of urls and iterate through it and than append the data somewhere?
I hope someone can help me. I would really appreciate it.
Thanks in advance.