TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✗ |
Maintained by Julien.
Swiftizy is a big help to develop Swift application using CoreData and consume REST service.
A lot of easy method for fetching, create, delete, predicate, and a PrimaryKey system.
Make GET, POST and DELETE easily
Just one line to convert your JSON in NSManagedObject subclass. No need to define key for each attributes.
You use CoreData and NSManagedObject subclass
You consume a REST service sending JSON and/or you want POST some JSON object on your server
You want to write the JSON object in your CoreData context with some control on these
You need to translate your app.
In the next version (1.3) will add:
Swiftizy is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Swiftizy"
import Swiftizy
To use the CoreDataManager, you need first to create your own CoreData stack, declare it in AppDelegate and give your managed context to the CoreDataManager in the
lazy var coreDataStack = CoreDataStack()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
CoreDataManager.managedContext = coreDataStack.context
return true;
}
First, you need to initialize your Translations system with the good language. If you allow manually language change in your app, just call again ‘initialize’ method with new value.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Translations.initialize(defaultLang: "en", currentLang: "fr")
return true;
}
After, in some controller, you can easily translate yours components
labelSignIn.text = Translations.translate("label_sign_in")
You just need to write the traduction file for each language you want in your app.
labelSignIn=Sign in
buttonConnection=Connection
labelSignIn=Connectez-Vous
buttonConnection=Connexion
The PlistReader is a simple class for read a properties list. You need to init the PlistReader with the file you want read and execute the getField method for get your value
This work only with String field for the moment, it’s used for save some information like a webservice url or something else.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
plistReader(plistFileName: "properties")
// Get my rest URL
print("Your url: \(PlistReader.getField("url")!)")
return true;
}
You can clone the project on your desktop and open it for get the example project. Once open, just swap your project on “Swiftizy_example” instead “Swiftizy”
When you launch the app, you have 5 steps. Execute it in order:
Get books from OpenLibrary API, with createElseReturn method: create object in CoreData if they dont, else return the object.
Verify everything is OK
Vizualize in the tableview your date: touch a row for more informations
Change an attribute of each book (All number_of_pages will be 10)
Get same book from step 1, but with createElseUpdate method: create object in CoreData, else update the object with the value in Json
Some informations will be displayed in the logs
When the configuration is complete and your datamodel / subclass are create, you can use the CoreDataManager: a static class who provide you a lot of method for CoreData
let newUser = CoreDataManager.Create.entity("User") as! User
newUser.firstname = "Jhon"
CoreDataManager.save()
You create with a verification if one attribute exist for dont duplicate same object. If exist, return nil
CoreDataManager.Create.exceptIfStringMatch("User", attributeName: "firstname", attributeValue: "Jhon")
let users: [User] = CoreDataManager.Fetch.all("User") as! [User]
With a define predicate
let users: [User] = CoreDataManager.Fetch.equalString("User", attributeName: "firstname", attributeValue: "Jhon") as [User]
Fetching with yours predicates (and descriptors if needed)
let predicate: NSPredicate = NSPredicate(format: "age = %d", 27)
let predicate2: NSPredicate = NSPredicate(format: "firstname = %@", "Jhon")
let predicates: NSCompoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate, predicate2])
let users: [User] = CoreDataManager.Fetch.custom("User", descriptors: nil, predicate: predicates) as! [User]
CoreDataManager.Fetch.orderBy("User", orderBy: "firstname", ascending: true)
Batch delete
CoreDataManager.Delete.batch("User")
Delete one
let user = CoreDataManager.Fetch.all("User")[0] as! User
CoreDataManager.Delete.one(user)
Delete many
let users = CoreDataManager.Fetch.all("User") as! [User]
CoreDataManager.Delete.many(users)
CoreDataManager.save()
CoreDataManager.reset()
CoreDataManager.discardChanges()
JsonParser give you one method to consume some Json and directly create the managedObject and put information correctly in your object. As all Json parser, you just need the name of attributes of your entity match with the json file. You can associate this with a GET request to get all book from a library. Use the RestManager to perform a GET request. You have different version of the GET request, take the good version you need.
let url = "http://mywebservice.com/rest/book/all"
RestManager.GET(url, withBehaviorResponseForArray: {(books, error) in
if error == nil {
for book in books {
// For return the object if already in CoreData (based on the primary key)
let myBook = JsonParser.jsonToManagedObject(object, createElseReturn: Book.self)
// For update the object with values in json if object is already in CoreData
let myBook = JsonParser.jsonToManagedObject(object, createElseUpdate: Book.self)
}
CoreDataManager.save()
}
})
Object Parser can parse a managedObject in jsonString. You can combine that with a POST method. You use the POST method, or the POST_SYNCRO, if you want synchronous request. If the post response with some JSON, you get the response in a dictionary, like for a GET request. If response is not in json, the data is convert in string and be accessible with the key “dataString”. Swiftizy have a loot of extension for string. Here, the POST response with a BOOLEAN, so you can use the .toBool() method in the dataString.
let url= "http://mywebservice.com/rest/book/post"
let aBook = CoreDataManager.fetchWithPredicateWithString("Book", attributeName: "title", attributeValue: "Harry Potter")[0] as! User
let jsonBook = ObjectParser.convertToJsonString(aBook)
RestManager.POST(url, jsonToPost: jsonBook, responseHandler: {(response, error) in
if error == nil {
if (response["dataString"] as! String).toBool() {
print("Success")
} else {
print("Post Failed, server return false)")
}
}
})
Julien Henrard, [email protected]
Swiftizy is available under the MIT license. See the LICENSE file for more info.