What we will cover
- Creating a new project.
- Grand Central Dispatcher
- How to fetch data from web using Swift.
- Tweet Functionality
- Run in Simulator
Create a new project
- Open Xcode
- File > New Project > Single Page Application
- ProductName : TriviaApp
- Language : Swift
- Device : Iphone
- Next and save the project
Grand Central Dispatcher
GCD (Not greatest common divisor) is a technology built by apple for efficiently using the multi core processors on IOS and OS X for improving the performance of the app.
Apps tend to become unresponsive when we perform tasks which take a long time like fetching data from a web server in the main thread. Ideally the main thread should be for handling touch events and reacting to the users events in realtime.
GCD help by pushing the slow running tasks to a background job queue to be executed concurrently without blocking the main thread.
GCD provide abstraction over the thread pool interface and helps user in writing code easily without worrying much about the the concurrency model.
How to fetch data from web using Swift.
There are different ways of fetching the data from the web but in our case we will use NSURLSession.sharedSession() to have singleton object.
A singleton class returns the same instance no matter how many times an application requests it. A typical class permits callers to create as many instances of the class as they want, whereas with a singleton class, there can be only one instance of the class per process. A singleton object provides a global point of access to the resources of its class. Singletons are used in situations where this single point of control is desirable, such as with classes that offer some general service or resource.
Lets implement the randomFact() function which will return a random fact each time we call the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
These tasks are executed in the background
- First we create a baseURL which point to the api which return a random fact
- Using the session object(Shared) we create a downloadTask to fetch the data and pass a completionHandler that will be called when the data is ready to be used
- CompletionHandler has the following semantics. (location: where the data is stored locally, response : response from the web call, error : in case there are any errors)
- The first thing we look at is if there is any error, if not we continue using the data by passing it to NSData and eventually to NSString.
These tasks occur in the main thread.
- Get the reference to the main queue and pass it to the dispatch_async queue
- Execute necessary code in the main thread for updating the UIView.
The tweet functionality is provided by the Social Framework introduce in IOS. However to use this feature we need to add the Social.Framework to our app. Click on the target button on the left side bar of the xcode.
The Social framework (Social.framework) provides a simple interface for accessing the user’s social media accounts. This framework supplants the Twitter framework and adds support for other social accounts, including Facebook, Sina Weibo, and others. Apps can use this framework to post status updates and images to a user’s account. This framework works with the Accounts framework to provide a single sign-on model for the user and to ensure that access to the user’s account is approved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Clone and run the project
You can find the code on Github. Clone the project and run https://github.com/sarchak/TriviaApp