Objective-C-2 Flashcards
(104 cards)
How do dispatch queues work?
1) Create a queue or use the provided system queue (global)
2) add a task to the queue, either async or sync by calling dispatch_async() or dispatch_sync()
3) The queues operate in FIFO order
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSLog(@"Block for asynchronous execution");
});
How many concurrent dispatch queues does the system provide?
Four global, differentiated by priority level.
DISPATCH_QUEUE_PRIORITY_DEFAULT
DISPATCH_QUEUE_PRIORITY_HIGH
DISPATCH_QUEUE_PRIORITY_LOW
DISPATCH_QUEUE_PRIORITY_BACKGROUND
Create a queue: dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
Why might you use autorelease in an ARC context?
If in a block of code you’re allocating lots of Objective-C objects surround parts of the code with autorelease blocks to free memory.
Why use serial queues?
To ensure statements execute in specified order.
Create a custom serial queue
dispatch_queue_t queue;
queue = dispatch_queue_create(“com.example.MyQueue”, NULL);
How do you test the identity of a queue (for debugging)?
dispatch_get_current_queue. Call from inside a block object to return the queue to which the block was submitted.
What is the concurrent looping construct available?
dispatch_apply (or dispatch_apply_f)
Add a cleanup function to a serial queue?
dispatch_set_finalizer_f
dispatch_queue_t serialQueue = dispatch_queue_create(“com.example.CriticalTaskQueue”, NULL);
dispatch_set_context(serialQueue, data);
dispatch_set_finalizer_f(serialQueue, &myFinalizerFunction);
What does the cleanup function do?
executes and cleans up context data associated with the queue (only called if the context pointer isn’t NULL)
Replace the following loop with a concurrent loop:
for (i = 0; i < count; i++) {
printf(“%u\n”,i);
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(count, queue, ^(size_t i) {
printf(“%u\n”,i);
});
How do you perform tasks on the main queue?
dispatch_get_main_queue
You can get the dispatch queue for your application’s main thread by calling the dispatch_get_main_queue function. Tasks added to this queue are performed serially on the main thread itself.
Using Grand Central Dispatch (GCD) you have an automatic autorelease pool inside the executing block. Would you need another one for any reason?
Yes, if you want to immediately release objects at the end of each loop for example, surround it with autorelease block.
How do you suspend a queue?
call dispatch_suspend then dispatch_resume. These calls must match.
Why would you want to use Dispatch Groups instead of Dispatch Queue?
Use dispatch groups when you want to accomplish a set of work and cannot progress until it’s complete.
Dispatch groups block a thread until one or more tasks are complete
Instead of dispatching tasks to a queue using the dispatch_async function, you use the dispatch_group_async function instead. This function associates the task with the group and queues it for execution.
What are the steps required to use a dispatch group?
1) get the global queue:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
2) Create the dispatch group:
dispatch_group_t group = dispatch_group_create();
3) Add the group to the queue:
dispatch_group_async(group, queue, ^{
// Some asynchronous work
});
4) Wait on the group:
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
5) Release the group:
dispatch_release(group);
What is the GCD synchronous call to dispatch a block to a queue?
dispatch_sync
dispatch_sync(queue, ^{
NSLog(@”HELLO”);
});
What is basic Class interface syntax?
@interface MyClass:NSobject
@end
How do you declare private variables in a class without using a private keyword? What are private variables known as?
Put them in the implementation file inside curly braces:
@implementation Person{
int age;
NSString *name;
}
Known as iVars.
Can you create a private variable in a class with a private keyword? public?
Yes, use the @private, @protected, or @public keywords. These are visibility modifiers though - two instances of a class can access all of each others variables, including private, no issues. These modifiers only affect the INHERITED classes.
Declare the variables in the implementation (to hide them), inside curly braces:
@implementation SampleClass{
NSMutableArray *myArr; @private int test; @public int test2; } @end
Can you use self to access an iVar?
No, self is only for properties. Refer directly to an iVar.
Why wouldn’t you put iVars into the interface file?
1) exposes implementation to end user
2) adding/changing/removing an iVar from the header makes compilation take longer: each .m file that imports the interface needs recompilation.
What is the default when a @property is declared in terms of properties?
readwrite
strong
atomic
getter/setter synthesized automatically
Are these declarations the same?
@property NSArray *name;
@property (strong, atomic, readwrite) NSArray *name;
yes
What should a primitive type attribute be set to?
Assign