I’m developing an iOS 5 and above with latest SDK.
I have to implement a synchronized method with a Thread Lock in Objective C.
This is the Java version of what I have to do:
public abstract class MyClass
{
[ ... ]
private static Object dataLock = new Object();
public static long dataId = 0;
[ ... ]
public static void PostData(byte[] data)
{
synchronized (MyClass.getDataLock())
{
dataId++;
MyClass.getDataLock().notify();
}
}
public static byte[] GetData()
{
synchronized (MyClass.getDataLock())
{
try
{
MyClass.getDataLock().wait();
}
catch (InterruptedException ex)
{}
return MyClass.getData();
}
}
[ ... ]
}
How do I implement dataLock in Objective C? As a NSObject?
How can I do PostData and GetData methods in Objective C?
Use the
NSConditionclass. E.g.:See the Thread Programming Guide for more info.