Latest 0.5.7
Homepage https://github.com/iOSWizards/AwesomeData
License MIT
Platforms ios 8.0, watchos 2.0, tvos 9.0
Authors

Fetches data, parse JSON and organise CoreData easily. Supported on iOS, watchOS and tvOS. :iphone: :watch: :tv:

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.7'

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.7'

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.5'

end

Author

Evandro Hoffmann, [email protected]

Contributor

Leonardo Kaminski Ferreira, [email protected]

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ā€¦

Latest podspec

{
    "name": "AwesomeData",
    "version": "0.5.7",
    "summary": "Handling Coredata has been made easy.",
    "description": "Fetch data from URLs, parse JSON and save to Coredata easily.",
    "homepage": "https://github.com/iOSWizards/AwesomeData",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Evandro Harrison Hoffmann, Leonardo Kaminski Ferreira": "[email protected], [email protected]"
    },
    "source": {
        "git": "https://github.com/iOSWizards/AwesomeData.git",
        "tag": "0.5.7"
    },
    "platforms": {
        "ios": "8.0",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "ios": {
        "source_files": "AwesomeData/Classes/**/*.{swift}"
    },
    "watchos": {
        "source_files": [
            "AwesomeData/Classes/AwesomeData.swift",
            "AwesomeData/Classes/Cache/*.swift",
            "AwesomeData/Classes/CoreData/*.swift",
            "AwesomeData/Classes/Fetcher/*.swift",
            "AwesomeData/Classes/Parser/*.swift"
        ]
    },
    "tvos": {
        "source_files": "AwesomeData/Classes/**/*.{swift}"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This