Objective-C-2 Flashcards
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
Describe an appropriate parent->child relationship?
Parent has strong pointer to child and child shouldn’t have strong back to parent or there’s a retain cycle.
What does the copy attribute do?
A variable/property with the copy attribute does not get a reference to the original assignment. It gets a copy of the value of that reference. When the actual referenced variable is changed it does not get reflected in the copy variable.
@property (nonatomic,copy) NSString *lastName;
NSString *str = @"HELLO"; model.lastName = str; // model.lastname = HELLO
str = @"Goodbye" //model.lastName still = HELLO, reference doesn't change it
What does the atomic attribute do?
prevents concurrent access to an object by multiple threads.
What does nonatomic attribute do?
No guarantee what happens if value is accessed simultaneously from different threads.
Is it faster to access an atomic or non-atomic property?
Faster for non-atomic
What are the two access attributes?
readonly, readwrite.
What does weak attribute signify? What are they best used on?
keep the object in memory as long as someone else holds a strong pointer to it. When the other that holds the strong pointer is set to nil, if the retain count of the object is 0, then the weak pointer is set to nil and the object is destroyed.
Delegates
IBOutlets, Subviews- (strongly held by superview)
Declare a weak IBOutlet to a UILabel?
__weak IBOutlet UILabel *titleLabel
Declare a weak property to a delegate of type SampleDelegate
@property (weak, nonatomic) id sDelegate;
What is unsafe_unretained?
It specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.. ARC doesn’t maintain control over memory of unsafe-unretained variables.
How to set a custom name for a getter and setter?
@property (nonatomic, strong, getter=getTestProperty, setter=setTestProperty:) NSString *testProperty;
Define the custom methods in the header and implement in implementation.
What must you do to write a custom getter and setter when declaring a property?
Ensure nonatomic
@interface Customer
@property (nonatomic) NSString* name;
@end
//Synthesize assigning a new name w/underscore @synthesize name = _name; //Must do this
//Implement the methods //Setter method - (void) setName:(NSString *)n { NSLog(@"Setting name to: %@", n);
_name = [n uppercaseString]; } //Getter method - (NSString*) name { NSLog(@"Returning name: %@", _name); return _name; }
What does the following mean: @synthesize mushroom = _mushroom;
@synthesize tells the compiler that _mushroom backs up the property mushroom for the mushroom and setMushroom methods, that instance variable should be created if not yet.
@interface Badger : NSObject ()
@property (nonatomic) Mushroom *mushroom;
@end
@implementation Badger; @synthesize mushroom = _mushroom; - (Mushroom *)mushroom { return _mushroom; } - (void)setMushroom:(Mushroom *)mush { _mushroom = mush; }
Given the following declaration how can you access and set the name property?
@property (nonatomic,strong) NSString* name;
Which is direct access and which is through the auto-generated setter and getter methods?
_name = @”HELLO”;
OR
self.name = @”HELLO”;
self uses the setter
_name directly accesses the property
Declare a static class method that takes in an NSString variable, void return?
\+ (void) myStaticFunction:(NSString *)myStr{ //do stuff }
Declare a static variable of type NSString named url? Where does it go, the Implementation or Interface?
static NSString *url = @”something”;
Goes in the implementation outside the curly braces.
@implementation SampleClass{
NSMutableArray *myArr; @private int test; @public int test2;
}
static NSString *url = @”something”;
Whats the difference between Static and Extern keywords?
Static makes methods and variables globally visible in a single file.
Extern makes them globally visible across all files.