SaveResult Polish #33

Closed
opened 2025-12-29 15:22:40 +01:00 by adam · 2 comments
Owner

Originally created by @PaulWoodIII on GitHub (Jan 17, 2016).

Looking at the code the SaveResult seems a little off

Here why is the save result optional?

public static func beginSynchronous(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? {        
    return self.defaultStack.beginSynchronous(closure)
}

Your implementation always returns a result from what I can see.

Also you might be inspired by Alamofire's Result enum: isSuccess and isFailure can be helpful to some:

990fded98a/Source/Result.swift

Originally created by @PaulWoodIII on GitHub (Jan 17, 2016). Looking at the code the SaveResult seems a little off Here why is the save result optional? ``` public static func beginSynchronous(closure: (transaction: SynchronousDataTransaction) -> Void) -> SaveResult? { return self.defaultStack.beginSynchronous(closure) } ``` Your implementation always returns a result from what I can see. Also you might be inspired by Alamofire's Result enum: isSuccess and isFailure can be helpful to some: https://github.com/Alamofire/Alamofire/blob/990fded98afe5135dc418e1f6eb0287027dd067f/Source/Result.swift
adam added the question label 2025-12-29 15:22:40 +01:00
adam closed this issue 2025-12-29 15:22:40 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jan 18, 2016):

@PaulWoodIII The documentation for beginSynchronous(...) explains why it's optional:

/**
...
 - returns: a `SaveResult` value indicating success or failure, or `nil` if the transaction was not committed synchronously
*/

So if you don't call commit() inside the transaction block (i.e. if you abandon/cancel the transaction), you are guaranteed to get a nil result.

As for Alamofire's isSuccess and isFailure, SaveResult already implements BooleanType, so you are free to check the result as so:

transaction.commit { result in
    if result {
        // ... succeeded
    }
    else {
        // ... failed
    }
}
@JohnEstropia commented on GitHub (Jan 18, 2016): @PaulWoodIII The documentation for `beginSynchronous(...)` explains why it's optional: ``` swift /** ... - returns: a `SaveResult` value indicating success or failure, or `nil` if the transaction was not committed synchronously */ ``` So if you don't call `commit()` inside the transaction block (i.e. if you abandon/cancel the transaction), you are guaranteed to get a `nil` result. As for Alamofire's isSuccess and isFailure, `SaveResult` already implements `BooleanType`, so you are free to check the result as so: ``` swift transaction.commit { result in if result { // ... succeeded } else { // ... failed } } ```
Author
Owner

@PaulWoodIII commented on GitHub (Jan 18, 2016):

Okay I get it now the extra code there really helps as well

@PaulWoodIII commented on GitHub (Jan 18, 2016): Okay I get it now the extra code there really helps as well
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#33