iOS Developer Library

Developer

Foundation Framework Reference NSConditionLock Class Reference

Options
Deployment Target:

On This Page
Language:

NSConditionLock

The NSConditionLock class defines objects whose locks can be associated with specific, user-defined conditions. Using an NSConditionLock object, you can ensure that a thread can acquire a lock only if a certain condition is met. Once it has acquired the lock and executed the critical section of code, the thread can relinquish the lock and set the associated condition to something new. The conditions themselves are arbitrary: you define them as needed for your application.

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.
  • init(condition:) - initWithCondition: Designated Initializer

    Initializes a newly allocated NSConditionLock object and sets its condition.

    Declaration

    Swift

    init(condition condition: Int)

    Objective-C

    - (instancetype)initWithCondition:(NSInteger)condition

    Parameters

    condition

    The user-defined condition for the lock. The value of condition is user-defined; see the class description for more information.

    Return Value

    An initialized condition lock object; may be different than the original receiver.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • condition condition Property

    The condition associated with the receiver. (read-only)

    Declaration

    Swift

    var condition: Int { get }

    Objective-C

    @property(readonly) NSInteger condition

    Discussion

    If no condition has been set, returns 0.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Attempts to acquire a lock before a specified moment in time.

    Declaration

    Swift

    func lockBeforeDate(_ limit: NSDate) -> Bool

    Objective-C

    - (BOOL)lockBeforeDate:(NSDate *)limit

    Parameters

    limit

    The date by which the lock must be acquired or the attempt will time out.

    Return Value

    YEStrue if the lock is acquired within the time limit, NOfalse otherwise.

    Discussion

    The condition associated with the receiver isn’t taken into account in this operation. This method blocks the thread’s execution until the receiver acquires the lock or limit is reached.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Attempts to acquire a lock.

    Declaration

    Swift

    func lockWhenCondition(_ condition: Int)

    Objective-C

    - (void)lockWhenCondition:(NSInteger)condition

    Parameters

    condition

    The condition to match on.

    Discussion

    The receiver’s condition must be equal to condition before the locking operation will succeed. This method blocks the thread’s execution until the lock can be acquired.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Attempts to acquire a lock before a specified moment in time.

    Declaration

    Swift

    func lockWhenCondition(_ condition: Int, beforeDate limit: NSDate) -> Bool

    Objective-C

    - (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit

    Parameters

    condition

    The condition to match on.

    limit

    The date by which the lock must be acquired or the attempt will time out.

    Return Value

    YEStrue if the lock is acquired within the time limit, NOfalse otherwise.

    Discussion

    The receiver’s condition must be equal to condition before the locking operation will succeed. This method blocks the thread’s execution until the lock can be acquired or limit is reached.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Attempts to acquire a lock without regard to the receiver’s condition.

    Declaration

    Swift

    func tryLock() -> Bool

    Objective-C

    - (BOOL)tryLock

    Return Value

    YEStrue if the lock could be acquired, NOfalse otherwise.

    Discussion

    This method returns immediately.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Attempts to acquire a lock if the receiver’s condition is equal to the specified condition.

    Declaration

    Swift

    func tryLockWhenCondition(_ condition: Int) -> Bool

    Objective-C

    - (BOOL)tryLockWhenCondition:(NSInteger)condition

    Return Value

    YEStrue if the lock could be acquired, NOfalse otherwise.

    Discussion

    As part of its implementation, this method invokes lockWhenCondition:beforeDate:. This method returns immediately.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

    See Also

    – tryLock

  • Relinquishes the lock and sets the receiver’s condition.

    Declaration

    Swift

    func unlockWithCondition(_ condition: Int)

    Objective-C

    - (void)unlockWithCondition:(NSInteger)condition

    Parameters

    condition

    The user-defined condition for the lock. The value of condition is user-defined; see the class description for more information.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • name name Property

    The name associated with the receiver.

    Declaration

    Swift

    var name: String?

    Objective-C

    @property(copy) NSString *name

    Discussion

    You can use a name string to identify a condition lock within your code. Cocoa also uses this name as part of any error descriptions involving the receiver.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.