Swift post in background with indicator

I am trying to create a social application where users can post.

It has a tableView that has 2 sections

0: depending posts
1: posts

Post data looks like this:

id: Int
post: String
uploading: Bool = true

If the user clicks on “Write a new post”, a new View Controller appears, that VC handles the post uploading

When the user sends his post, it gets added to the depending posts immediately, then the app pops to the root VC, + It starts an URL session

self.dismiss(animated: true, completion: {
    PostFunctions.shared.post(){
        // ... code ...
        if response == 1 {
            // move post from depending to normal
        } else {
            // set the post’s uploading bool to false
        }
        // send notification to the main vc to reload its tableView
    }
})

This code uploads the post to the web. If either the web connection fails, or my site responds not accepted, then the post in my app changes it’s uploading value to false

After that the tableView gets reloaded with a Notification


This way I know if the post failed to upload (it is in depending posts and its uploading bool is set to false). In this case I present the user a button to try again “Failed. Click to try again.

If the uploading is set to true then I display a label in the cell saying “posting...

Aaaand if the post gets accepted, the I remove it from the depending posts and I add it to the normal posts


My question is, that am I doing it right?

Or what would you recommend?

Currently I’m using this method and it works fine.


Images:

enter image description here enter image description here

1 answer

  • answered 2018-08-15 17:13 John Ayers

    I don't see any issue with this solution. It looks like you're doing the right thing by turning over the data to URL Session and letting that happen in the background. The only change I might make would be to include an activity indicator that could be turned off using a completion closure at the conclusion of the URL Session. Arguably you're already monitoring that, so you could implement fairly easily.