Swift nested functions vs Closure Variables - swift

I want to make a function that fetches a record from CloudKit, if it encounters a temporary network error the function should retry.
func fetchRecord(withRecordID recordID: CKRecordID, returnBlock: (optError: ErrorType?) -> Void){
func internalReturnBlock(optError optError: ErrorType?){
if NSThread.isMainThread() {
returnBlock(optError: optError)
}
else{
dispatch_async(dispatch_get_main_queue(), {
returnBlock(optError: optError)
})
}
}
func internalWork(){
privateDB.fetchRecordWithID(recordID) { (optRecord, optError) in
if let error = optError{
// IF NETWORK ERROR RETRY
internalWork()
}
else{
internalReturnBlock(optError: nil)
}
}
}
internalWork()
}
Here I define such function (simplified), If the fetch encounters an error it retries by calling the nested function internalWork()
My question is what would be the difference between using nested functions or creating local closure variables?
For example, here I change the internalReturnBlock to a closure variable:
func fetchRecord2(withRecordID recordID: CKRecordID, returnBlock: (optError: ErrorType?) -> Void){
var internalReturnBlock = { (optError: NSError?) in
if NSThread.isMainThread() {
returnBlock(optError: optError)
}
else{
dispatch_async(dispatch_get_main_queue(), {
returnBlock(optError: optError)
})
}
}
func internalWork(){
privateDB.fetchRecordWithID(recordID) { (optRecord, optError) in
if let error = optError{
// IF NETWORK ERROR RETRY
internalWork()
}
else{
internalReturnBlock(nil)
}
}
}
internalWork()
}
What are the differences between using a nested function vs a variable block?
Any advantages or problems?

There is no difference in effect. One is a declared function with a name, the other is anonymous. But they are both functions. And a function is a closure in Swift, so they are both closures.
An anonymous function is permitted to use some abbreviations of form, such as the omission of return in a one-liner that returns a value. But none of those abbreviations makes any ultimate effective difference.
However, an anonymous function in Swift has one feature a declared function does not — a capture list. This can help avoid retain cycles.
f {
[unowned self] in
return self.name
}
Also, an anonymous function is defined after the declaration of the function that takes it as a parameter, so it can use terms that appear in that declaration:
f(param:String) {
return param
}
But if you are not using those features, then it doesn't matter which you use. They work identically.

Related

facing issue while calling nested function in swift ios

func check(a: () -> ()){
func a(){
print("A Calling")
}
a()
}
Calling function
check {
print("Check Calling")
}
In the above code, i am not able to call a() function and it will directly be executing "check calling "
Result : "Check Calling"
When i am calling check function it should be execute a() function also and order should be
Result should be:
"A Calling"
"Check Calling"
The problem with the code in your question is that you do not call the nested function at all. It doesn't help that you've named it the same as the passed in function which is perhaps why you are having trouble isolating the issue.
This works
func check(completion: () -> Void) {
func localFunction() {
print("A Calling")
}
localFunction()
completion()
}
check {
print("Check Calling")
}
// Outputs:
//
// A Calling
// Check Calling

Swift closures - force a closure to always complete

Is it possible to force a closure to be completed? In the same way that a function with a return value MUST always return, it would be ace if there was a way to force a closure to contain the syntax necessary to always complete.
For example, this code will not compile because the function does not always return a value:
func isTheEarthFlat(withUserIQ userIQ: Int) -> Bool {
if userIQ > 10 {
return false
}
}
In the exact same way, I would like to define a function with a closure, which will also not compile if the closure never returns. For example, the code below might never return a completionHandler:
func isTheEarthFlat(withUserIQ userIQ: Int, completionHandler: (Bool) -> Void) {
if userIQ > 10 {
completionHandler(false)
}
}
The code above compiles, but I was wondering if there is a keyword which enforces that the closure sends a completion handler in all cases. Maybe it has something to do with the Void in the above function?
No, there is no language construct that will result in a compiler error if you forget (or don't need) to call the completion handler under all possible conditions like a return statement.
It's an interesting idea that might make a useful enhancement to the language. Maybe as a required keyword somewhere in the parameter declaration.
There is no special keyword for what you want. But there is an interesting approach you can take into consideration, that won't compile:
func isTheEarthFlat(withUserIQ userIQ: Int, completionHandler: (Bool) -> Void) {
let result: Bool
defer {
completionHandler(result)
}
if userIQ > 10 {
result = false
}
}
that will do and is completionHandler is forced to be called:
func isTheEarthFlat(withUserIQ userIQ: Int, completionHandler: (Bool) -> Void) {
let result: Bool
defer {
completionHandler(result)
}
if userIQ > 10 {
result = false
} else {
result = true
}
}
Not sure it's a good pattern to use.
Here is an interesting technique I thought of. You define GuarenteedExecution and GuarenteedExecutionResult types.
A GuarenteedExecution is a wrapper around a closure, which is to be used in a context where the execution of the closure must be guaranteed.
The GuarenteedExecutionResult is the result of executing a GuarenteedExecution. The trick is to have a desired function, e.g. isTheEarthFlat, return a GuarenteedExecutionResult. The only way to obtain a GuarenteedExecutionResult instance is by calling execute(argument:) on a GuarenteedExecution. Effectively, the type checker features responsible for guaranteeing a return, are now being used to guarantee the execution of GuarenteedExecution.
struct GuarenteedExecutionResult<R> {
let result: R
fileprivate init(result: R) { self.result = result }
}
struct GuarenteedExecution<A, R> {
typealias Closure = (A) -> R
let closure: Closure
init(ofClosure closure: #escaping Closure) {
self.closure = closure
}
func execute(argument: A) -> GuarenteedExecutionResult<R> {
let result = closure(argument)
return GuarenteedExecutionResult(result: result)
}
}
Example usage, in a seperate file (so as to not have access to GuarenteedExecutionResult.init):
let guarenteedExecutionClosure = GuarenteedExecution(ofClosure: {
print("This must be called!")
})
func doSomething(guarenteedCallback: GuarenteedExecution<(), ()>)
-> GuarenteedExecutionResult<()> {
print("Did something")
return guarenteedCallback.execute(argument: ())
}
_ = doSomething(guarenteedCallback: guarenteedExecutionClosure)

How to utilize PromiseKit to ensure a queried object has been retrieved before proceeding?

I am programming an app which utilizes a parse-server (hosted by heroku) database. I have several functions which pull information from the DB, but they are all inherently asynchronous (because of the way parse's .findObjectinBackground works.) The issue with this as that the later DB queries require information from previous queries. Since the information being pulled is asynchronous, I decided to implement PromiseKit to ensure that the object has been found from findObjectinBackground from the first query, before running the second query.
The general form of the queries is as follows:
let query = PFQuery(classname: "Hello")
query?.findObjectsInBackground(block: { (objects, error) in
if let objectss = objects{
for object in objectss{ //object needs to be pulled
arrayOfInterest.append(object)
//array must be appended before moving on to next query
}
}
})
I just do not know how exactly to do this. This is the way I would like to implement it:
import PromiseKit
override func viewDidLoad(){
when(/*query object is retrieved/array is appended*/).then{
//perform the next query
}
}
I simply don't know exactly what to put in the when() and the .then{}. I tried making the queries into their own individual functions and calling them inside those two (when and then) functions, but I basically get told that I cannot because they return void. Also, I cannot simply ensure the first query is run in the when() as the query.findObjectinBackground(in the query) being asynchronous is the issue. The object specifically needs to be pulled, not just the query run, before the next one can fire.
Do you want create your promise?
You need write a function that return a Promise<Any>. In your case, need to encapsulate the entire code inside of Promise { fulfill, reject in HERE}. For example:
func foo(className: String) -> Promise<[TypeOfArrayOfInterest]> {
return Promise { fulfill, reject in
let query = PFQuery(classname: className)
query?.findObjectsInBackground(block: { (objects, error) in
if let error = error {
reject(error) // call reject when some error happened
return
}
if let objectss = objects {
for object in objectss{
arrayOfInterest.append(object)
}
fulfill(arrayOfInterest) // call fulfill with some value
}
})
}
}
Then, you call this function in firstly:
firstly {
foo(className: "Hello")
}.then { myArrayOfInterest -> Void in
// do thing with myArrayOfInterest
}.catch { error in
// some error happened, and the reject was called!
}
Also, I wrote a post in my blog about, among other things, PromiseKit and architecture. It may be helpful: http://macalogs.com.br/ios/rails/ifce/2017/01/01/experiencias-eventbee.html
Edit
More complete example:
func foo() -> Promise<Int> {
...
}
func bar(someText: String) -> Promise<String> {
...
}
func baz() -> Promise<Void> {
...
}
func runPromises() {
firstly {
foo()
}.then { value -> Promise<Any> in
if value == 0 {
return bar(someText: "no")
} else {
return bar(someText: "yes")
}
}.then { _ /* I don't want a String! */ -> Promise<Void> in
baz()
}.catch { error in
// some error happened, and the reject was called!
}
}
Or if you don't want a catch:
_ = firstly {
foo()
}.then { _ in
// do some thing
}
Swift have a greate type inference, but, when use PromiseKit, I recommend always write a type in then closure, to avoid erros.

Can swift exit out of the root closure?

In Swift, if I'm inside of a closure, that is itself inside of another function, is there a way to exit out of the function itself?
Here's an example of what this might look like using closures from the GCDKit library.
func test() {
GCDQueue.Default.async {
print("Print me!")
return //Is there a statement that does this?
}.notify(.Main) {
print("Never print me.")
}
}
No there is not. Closures run in self-contained environments. For all you know, by the time that closure is executed, the thread on which test() was called is no longer executing the test() method.
Let's consider a simpler version that doesn't include any third-party libraries, extra queues, or other complexity. We'll just create a closure and immediately execute it.
func dothing(andPrint shouldPrint: Bool) {
let closure = {
guard shouldPrint else { return }
print("I printed!")
}
closure()
print("did it return?")
}
dothing(andPrint: false) // Prints "did it return?"
The return here exits the closure, not dothing. Since closure could be passed to some other function, or stored in a property and executed at some later time (possibly on a different queue as in your example), there's no way for the return to exit anything beyond itself. Consider if we were to refactor the creation of the closure into its own function:
func fetchClosure(andPrint shouldPrint: Bool) -> () -> Void {
return {
guard shouldPrint else { return }
print("I printed!")
}
}
func dothing(andPrint shouldPrint: Bool) {
let closure = fetchClosure(andPrint: shouldPrint)
closure()
print("did it return?")
}
dothing(andPrint: false) // Prints "did it return?"
It shouldn't be surprising this has the same behavior (otherwise this wouldn't be a simple refactor). Now imagine how it would (or even could) work if return behaved any other way.
Your example is just a much more complicated version of the same thing. return exits the closure.

How to call a such swift function?

/* Checks Is Username Already Exists
Take - username
Return - Bool (True/False) */
func checkIsUserExists(username: String, completion: ((isUser: Bool?) -> Void)!) {
var isPresent: Bool = false;
let query: PFQuery = PFQuery(className: "your_class_name")
query.whereKey("your_key", equalTo:username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if (objects!.count > 0) {
isPresent = true;
}
} else {
// Log details of the failure
println("Error: \(error) \(error!.userInfo!)")
}
completion(isUser: isPresent);
}
}
I found this function method that checks if the user exists in parse database but I dont get it what is the proper way to call it?
checkIsUserExists("user#gm.com, comletion.. ?)
It requires a closure that acts as a callback. So you'd call it in the following way:
checkIsUserExists("User Name"){isUser in if let user = isUser where user {
userFound()
}
else {
userNotFound()
}
}
Assuming you also had the following methods in place:
func userFound() {
// do something if the user was found
}
func userNotFound() {
// do something if the user was not found
}
The important keyword is in. Before the keyword, within the curly braces, I've named the value that is being captured isUser. It is a Bool as we can see from the method. And the closure is not required to return anything: (isUser: Bool?) -> Void)! hence the word Void. After the in keyword we can use the captured value to do whatever we wish with the optional true or false value. Here I call one of two functions.
In terms of code it is a very poor callback because while we know whether the user exists by the time the callback happens there might be several callbacks waiting in line and so we won't know which user exists because the user name is not passed to the closure.
You can find out plenty about closures in the Swift documentation.
got it!
The correct way is
checkIsUserExists(userEmail.text!)
{
(isUser: Bool?) in
print(isUser)
}
WOW! what a weird function call