Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Is it okay to fix the ''escaping closure captures mutating 'self' parameter'' problem?
Next
Answered
Rowan Evans
  • 25
Rowan Evans
Asked: May 18, 20222022-05-18T19:08:53+00:00 2022-05-18T19:08:53+00:00In: Programs

Is it okay to fix the ”escaping closure captures mutating ‘self’ parameter” problem?

  • 25

. Advertisement .

..3..

. Advertisement .

..4..

I am working with programs and getting the error message:

Closure cannot implicitly capture a mutating self parameter

Here is the detail of the code that I ran:

FirebaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
  if let _ = snapshot.value as? NSNull {
  self.img = UIImage(named:"Some-image")!
  } else {
  self.img = UIImage(named: "some-other-image")!
  }
 })

I need an explanation for the problems I’ve encountered. How to fix escaping closure captures mutating ‘self’ parameter?

swift
  • 2 2 Answers
  • 472 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-21T03:10:21+00:00Added an answer on June 21, 2022 at 3:10 am

    The cause:

    FirebaseRef.observeSingleEvent(of:with:) is a struct, it’s also a type of value. In this case a mutating context doesn’t know that you are mutating and binding self in an @escaping closure. Therefore, the error happens.

    Solution:

    For short versions:

    You only need to update your current type to a reference one with class.

    For longer versions:

    You also can change your current type observeSingleEvent(...) into a reference type like a class, it’s better than a struct:

    class Foo {
    init() {}
    private func bar(with block: @escaping () -> ()) { block() }
    
    func bax() {
    bar { print(self) }
    }
    }

    If you want to capture self weakly or strongly, it’s depends on your case. For example:

    FirebaseRef.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in ...
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Alice Boulet
    2022-05-25T20:51:14+00:00Added an answer on May 25, 2022 at 8:51 pm

    Sync Solution

    If you want to modify a value type (struct), it may not work for async calls. However, it will work synchronously if you write it this way:

    struct Banana {
     var isPeeled = false
     mutating func peel() {
     var result = self
    
     SomeService.synchronousClosure { foo in
     result.isPeeled = foo.peelingSuccess
     }
    
     self = result
     }
    }

    A “mutating self”, with different value types, cannot be captured by any other means than providing a copy (hence var).

    Why not Async instead?

    This is because you can’t mutate result with no compiler error. However, you cannot return the mutated result to self. Despite the fact that there will be no error, self won’t change since the closure of peel() is not dispatched.

    You can try to circumvent this by changing your code to make the async calls synchronous. Wait for the call to complete. Although technically possible, it is unlikely that this will serve the purpose of the async API with which you are interacting. You should change your approach.

    Although technically sound, changing struct to class doesn’t solve the problem. As an example, the class Banana property can be changed synchronously by anyone-knows-when. This is difficult to understand and will lead to problems. It’s better to write an API handler outside of the model and then fetch and modify the model object upon completion. This is because it doesn’t have enough context. I assume that this is model code since self.img has been mutated in OP’s source code.

    It may be possible to add “async corruption” objects.

    This is what I am thinking of:

    • A BananaNetworkRequestHandler executes requests simultaneously and reports the result to a BananaStore
    • The BananaStore will then look for peelingResult.bananaID on its inside and take the Banana appropriate Banana.
    • After finding an object with banana.bananaID == peelingResult.bananaID it sets banana.isPeeled = peelingResult.isPeeled.
    • Finally, replace the original object by the mutated instance.

    It is easy to see that the search for a simple solution can quickly become complicated, especially if you need to change the architecture of an app.

    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.