To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 8 or Higher Swift-3
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
Evandro Hoffmann, [email protected]
Leonardo Kaminski Ferreira, [email protected]
AwesomeData is available under the MIT license. See the LICENSE file for more info.
Coredata code should never belong to AppDelegate file, so with the AwesomeData, you can have it from an external source.
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()
Fetch data from a URL with standard properties.
AwesomeFetcher.fetchData("YOUR URL STRING") { (data) in
//process data
}
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 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 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
}
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*
}
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"},
{...}]
Gets JSON Object from NSData
let jsonObject = AwesomeParser.jsonObject(data)
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…