Created
December 25, 2019 15:03
-
-
Save SergLam/f23d90fcf9fcfc161114cba0cbca0e0a to your computer and use it in GitHub Desktop.
CoreData example model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreData | |
import Foundation | |
// NOTE: use manual / none code generation - | |
// it give full controll over models properties(custom getters + setters) + model lifecycle | |
// To create a new model class: | |
// 1. Focus on core data model class | |
// 2. XCode -> Editor -> Create NSManagedObject Subclass | |
// 3. Select an appropriate model from database scheme and create a class | |
/** | |
User profile (id, email, phone, picture_url, social links and ect.) | |
*/ | |
@objc(MyProfileSettings) | |
class MyProfileSettings: NSManagedObject { | |
@discardableResult | |
convenience init(settings: MyProfileSettingsJSON, context: NSManagedObjectContext) { | |
let name = String(describing: type(of: self)) | |
guard let entity = NSEntityDescription.entity(forEntityName: name, in: context) else { | |
preconditionFailure("Unable to create NSEntityDescription") | |
} | |
self.init(entity: entity, insertInto: context) | |
self.email = settings.email | |
self.id = Int64(settings.id ?? 0) | |
self.name = settings.name | |
self.phone = settings.phone | |
self.picture = settings.picture | |
self.ridingStyle = setRidingStyles(settings.ridingStyle?.compactMap{ return $0.rawValue } ?? []) | |
guard let media = settings.social else { | |
return | |
} | |
let newMedia = UserSocialMedia(media: media, relationship: self, context: context) | |
self.media = newMedia | |
} | |
func setRidingStyles(_ styles: [String]) -> Data { | |
return NSKeyedArchiver.archivedData(withRootObject: styles) | |
} | |
func getRidingStyles() -> [RidingStyle] { | |
guard let data = self.ridingStyle else { | |
return [] | |
} | |
guard let result = NSKeyedUnarchiver.unarchiveObject(with: data) as? [String] else { | |
assertionFailure("Unable to unarchive data") | |
return [] | |
} | |
return result.compactMap{ RidingStyle(rawValue: $0) } | |
} | |
func getGender() -> Gender? { | |
let intValue = Int(self.gender) | |
guard let gender = Gender(rawValue: intValue) else { | |
return nil | |
} | |
return gender | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreData | |
import Foundation | |
extension MyProfileSettings { | |
@nonobjc | |
class func fetchRequest() -> NSFetchRequest<MyProfileSettings> { | |
return NSFetchRequest<MyProfileSettings>(entityName: "MyProfileSettings") | |
} | |
@NSManaged var email: String? | |
@NSManaged var id: Int64 | |
@NSManaged var name: String? | |
@NSManaged var phone: String? | |
@NSManaged var picture: String? | |
@NSManaged var ridingStyle: Data? | |
@NSManaged var gender: Int64 | |
@NSManaged var media: UserSocialMedia? | |
@NSManaged var parent: MyUser? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment