Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

ios - What is the modern approach for Core Data Property Validation? Does Key-Value Validation still the de facto approach?

As the Core Data Programming Guide - Object Validation, updated to Swift 3, suggests that the Key-Value Validation utilizing the Key-Value Coding of objective-c runtime is the recommended approach to perform single property validation.

As the evolutions of Swift and iOS in recent years, does this approach still represent the best practice? And what are the practical caveats when applying this technic in modern iOS?

For example,

@objc(AuthorMO)
public class AuthorMO: NSManagedObject, Identifiable {
    @NSManaged public var uuid: UUID?
    @NSManaged public var name: String?
}

// MARK: Key-Value Property Validation
extension AuthorMO {
    @objc public func validateUuid(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>) throws {
        guard let newValue = value.pointee as? UUID else { return }
        // Custom property validation.
    }
    
    @objc public func validateName(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>) throws {
        guard let newValue = value.pointee as? String else { return }
        // Custom property validation.
    }
}

// MARK: LifeCycle Validation Alternative
// previously mainly used for inter-properties validation.
extension AuthorMO {
    public override func validateForInsert() throws {
        try super.validateForUpdate()
        try propertyValidations()
    }
    
    public override func validateForUpdate() throws {
        try super.validateForUpdate()
        try propertyValidations()
    }
    
    public func propertyValidations() throws {
        try validateUUID()
        try validateName()
    }
    
    public func validateUUID() throws {
        let newValue = primitiveValue(forKey: #keyPath(AuthorMO.uuid))
        // Custom property validation
    }
    
    public func validateName() throws {
        let newValue = primitiveValue(forKey: #keyPath(AuthorMO.name))
        // Custom property validation
    }
}

question from:https://stackoverflow.com/questions/65951874/what-is-the-modern-approach-for-core-data-property-validation-does-key-value-va

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is one way of doing it. It's only automatically used when you're saving changes, and in that case all it can do is prevent saving and return an error message. You might display that error message to the user. If that fits your needs, then it's a good approach. You can also run it manually. It might be less useful in other situations, for example importing data from a server API. This is the only built-in technique specifically designed for property validation.

There are other ways-- literally any way that works for you to check that the property values fit your requirements. For example, you could write your own failable initializer, and have it return nil if any of the arguments are no good. Or have it throw so it can return an Error explaining why it can't initialize. Custom code like this won't prevent saving changes, but if you use it carefully it'll still make sure values match your requirements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...