iOSWizards / AwesomeData

Fetches data, parse JSON and organise CoreData

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AwesomeData

Fetches data, parse JSON and organise CoreData easily. Supported on iOS, watchOS and tvOS. 📱 ⌚ 📺

Swift License Version License Platform

Continuous integration - branch master

CI Status Master

Continuous integration - branch develop

CI Status

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 8 or Higher Swift-3

Installation

AwesomeData is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "AwesomeData", git: 'https://github.com/iOSWizards/AwesomeData.git', tag: '0.5.8'

To use it on Apple Watch, simply add the following lines to your Podfile:

target 'YourAppleWatch Extension' do
    platform :watchos, '2.0'
    pod 'AwesomeData', git: 'https://github.com/iOSWizards/AwesomeData.git', tag: '0.5.8'
    
end

To use it on Apple TV too, simply add the following lines to your Podfile:

target 'YourAppleTVTarget' do
    platform :tvos, '9.0'
    pod 'AwesomeData', git: 'https://github.com/iOSWizards/AwesomeData.git', tag: '0.5.8'
    
end

Author

Evandro Hoffmann, evandro@itsdayoff.com

Contributor

Leonardo Kaminski Ferreira, leonardo.kaminski.ferreira@gmail.com

License

AwesomeData is available under the MIT license. See the LICENSE file for more info.

Removing Coredata code from AppDelegate

Coredata code should never belong to AppDelegate file, so with the AwesomeData, you can have it from an external source.

How to Use:

In AppDelegate:

  • Delete the coredata code (everything below func applicationWillTerminate(application: UIApplication))
  • Import the library in AppDelegate
import AwesomeData
  • Add to didFinishLaunchingWithOptions
AwesomeData.setDatabase("NAME OF YOUR DATABASE FILE WITHOUT EXTENSION")
//AwesomeData.showLogs = true
  • Replace / Add to applicationWillTerminate
AwesomeData.saveContext()

Fetching data from URL:

Simple Fetch:

Fetch data from a URL with standard properties.

AwesomeFetcher.fetchData("YOUR URL STRING") { (data) in
    //process data
}

Fetch with Method Type:

Fetch data from a URL with standard properties, but choosing the method type.

AwesomeFetcher.fetchData("YOUR URL STRING",
method: .GET/.POST/.PUT/.DELETE) { (data) in
    //process data
}

Fetch with Method Type and Authorization code:

Fetch data from a URL with authorization code.

AwesomeFetcher.fetchData("YOUR URL STRING",
method: .GET/.POST/.PUT/.DELETE,
authorization: "AUTHORIZATION CODE") { (data) in
    //process data
}

Fetch with Method Type and JSON Body:

Fetch data from URL with JSON Body

AwesomeFetcher.fetchData("YOUR URL STRING",
method: .GET/.POST/.PUT/.DELETE,
jsonBody: [String : AnyObject]?,
authorization: "AUTHORIZATION CODE") { (data) in
    //process data
}

The complete Fetch request:

Fetch data from URL with any combination of properties.

AwesomeFetcher.fetchData("YOUR URL STRING",
method: .GET/.POST/.PUT/.DELETE,
bodyData: NSData?,
headerValues: [[String]]?,
shouldCache: Bool, completion: { (data) in
*process data*
}

Parsing JSON data

Parse NSData into a Dictionary and parse to Coredata Object.

Let's consider this Unsplash JSON object:

[{
"format":"jpeg",
"width":5616,
"height":3744,
"filename":"0000_yC-Yzbqy7PY.jpeg",
"id":0,
"author":"Alejandro Escamilla",
"author_url":"https://unsplash.com/@alejandroescamilla",
"post_url":"https://unsplash.com/photos/yC-Yzbqy7PY"},
{...}]

NSData to JSON Dictionary

Gets JSON Object from NSData

let jsonObject = AwesomeParser.jsonObject(data)

Parsing JSON Dictionary to Coredata Object

Consider you have the UnsplashImage Coredata object, to parse it, use one of the parsing helpers:

parseInt(jsonObject, key: "KEY")
parseDouble(jsonObject, key: "KEY")
parseString(jsonObject, key: "KEY")
parseDate(jsonObject, key: "KEY")
parseBool(jsonObject, key: "KEY")
let objectId = parseInt(jsonObject, key: "id")

//gets object with objectId, to make sure there is only one object in Coredata with that ID. If it's nil, create a new object and use it.
if let unsplashImage = getObject(predicate: NSPredicate(format: "objectId == %d", objectId.intValue), createIfNil: true) as? UnsplashImage {
unsplashImage.objectId = objectId
unsplashImage.width = parseDouble(jsonObject, key: "width")
unsplashImage.height = parseInt(jsonObject, key: "height")
unsplashImage.format = parseString(jsonObject, key: "format")
unsplashImage.filename = parseString(jsonObject, key: "filename")
unsplashImage.author = parseString(jsonObject, key: "author")
unsplashImage.authorUrl = parseString(jsonObject, key: "author_url")
unsplashImage.postUrl = parseString(jsonObject, key: "post_url")
}

DOCUMENTATION UNDER DEVELOPMENT…

About

Fetches data, parse JSON and organise CoreData

License:MIT License


Languages

Language:Swift 95.8%Language:Ruby 4.2%