Coder Social home page Coder Social logo

Thread safety about afhttpsessionoperation HOT 4 CLOSED

pronebird avatar pronebird commented on June 13, 2024
Thread safety

from afhttpsessionoperation.

Comments (4)

robertmryan avatar robertmryan commented on June 13, 2024

Interesting question: I've never seen Apple advise that in custom accessor methods and it's not in any of their NSOperation subclass examples (neither in Configuring Operations for Concurrent Execution nor their new Swift Advanced NSOperations example).

from afhttpsessionoperation.

pronebird avatar pronebird commented on June 13, 2024

Well according to documentation:

isExecuting
isFinished

Your implementations of these methods must be safe to call from other threads simultaneously. You must also generate the appropriate KVO notifications for the expected key paths when changing the values reported by these methods.

I suppose same goes for setExecuting and setFinished because they access the same ivars.

from afhttpsessionoperation.

robertmryan avatar robertmryan commented on June 13, 2024

The "Multicore Considerations" section of the NSOperation class reference says something very similar:

Multicore Considerations

The NSOperation class is itself multicore aware. It is therefore safe to call the methods of an NSOperation object from multiple threads without creating additional locks to synchronize access to the object. This behavior is necessary because an operation typically runs in a separate thread from the one that created and is monitoring it.

When you subclass NSOperation, you must make sure that any overridden methods remain safe to call from multiple threads. If you implement custom methods in your subclass, such as custom data accessors, you must also make sure those methods are thread-safe. Thus, access to any data variables in the operation must be synchronized to prevent potential data corruption. For more information about synchronization, see Threading Programming Guide.

I've modified my code accordingly.

It's just fascinating that Apple universally ignores this advice in their own code.

from afhttpsessionoperation.

pronebird avatar pronebird commented on June 13, 2024

Multi-threading is hard. Thanks for update!

from afhttpsessionoperation.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.