Mac Developer Library

Developer

Foundation Framework Reference NSDistributedLock Class Reference

Options
Deployment Target:

On This Page
Language:

NSDistributedLock

The NSDistributedLock class defines an object that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file. More...

Inheritance


Conforms To


Import Statement


import Foundation @import Foundation;

Availability


Available in OS X v10.0 and later.
  • Returns an NSDistributedLock object initialized to use as the locking object the file-system entry specified by a given path.

    Declaration

    Objective-C

    + (NSDistributedLock *)lockWithPath:(NSString *)aPath

    Parameters

    aPath

    All of aPath up to the last component itself must exist. You can use NSFileManager to create (and set permissions) for any nonexistent intermediate directories.

    Return Value

    An NSDistributedLock object initialized to use as the locking object the file-system entry specified by aPath.

    Discussion

    For applications to use the lock, aPath must be accessible to—and writable by—all hosts on which the applications might be running.

    Import Statement

    Availability

    Available in OS X v10.0 and later.

  • init(path:) initWithPath: Designated Initializer

    Initializes an NSDistributedLock object to use as the lock the file-system entry specified by a given path.

    Declaration

    Swift

    init?(path aPath: String)

    Objective-C

    - (instancetype)initWithPath:(NSString *)aPath

    Parameters

    aPath

    All of aPath up to the last component itself must exist. You can use NSFileManager to create (and set permissions) for any nonexistent intermediate directories.

    Return Value

    An NSDistributedLock object initialized to use as the locking object the file-system entry specified by aPath.

    Discussion

    For applications to use the lock, aPath must be accessible to—and writable by—all hosts on which the applications might be running.

    Import Statement

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    + lockWithPath:

  • Attempts to acquire the receiver and immediately returns a Boolean value that indicates whether the attempt was successful.

    Declaration

    Swift

    func tryLock() -> Bool

    Objective-C

    - (BOOL)tryLock

    Return Value

    YEStrue if the attempt to acquire the receiver was successful, otherwise NOfalse.

    Discussion

    Raises NSGenericException if a file-system error occurs.

    Import Statement

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    – unlock

  • Forces the lock to be relinquished.

    Declaration

    Swift

    func breakLock()

    Objective-C

    - (void)breakLock

    Discussion

    This method always succeeds unless the lock has been damaged. If another process has already unlocked or broken the lock, this method has no effect. You should generally use unlock rather than breakLock to relinquish a lock.

    Even if you break a lock, there’s no guarantee that you will then be able to acquire the lock—another process might get it before your tryLock is invoked.

    Raises an NSGenericException if the lock could not be removed.

    Import Statement

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    – unlock

  • Relinquishes the receiver.

    Declaration

    Swift

    func unlock()

    Objective-C

    - (void)unlock

    Discussion

    You should generally use the unlock method rather than breakLock to release a lock.

    An NSGenericException is raised if the receiver doesn’t already exist.

    Import Statement

    import Foundation

    Availability

    Available in OS X v10.0 and later.

    See Also

    – breakLock

  • Returns the time the receiver was acquired by any of the NSDistributedLock objects using the same path.

    Declaration

    Swift

    @NSCopying var lockDate: NSDate { get }

    Objective-C

    @property(readonly, copy) NSDate *lockDate

    Return Value

    The time the receiver was acquired by any of the NSDistributedLock objects using the same path. Returns nil if the lock doesn’t exist.

    Discussion

    This method is potentially useful to applications that want to use an age heuristic to decide if a lock is too old and should be broken.

    If the creation date on the lock isn’t the date on which you locked it, you’ve lost the lock: it’s been broken since you last checked it.

    Import Statement

    import Foundation

    Availability

    Available in OS X v10.0 and later.