But now I'm back, and Yes, some interesting things has happened in my developing career (if you can call it that) latley:
1. I've started working at the Institution for informatics and media (My institution) at Uppsala University as an assistant at the introduction to programming class. The latest month I've had the privilege to hold lectures and helped students in C#. This is really a great experience and you learn a lot by teaching others to program. I got chosen not only for my skills as a programmer (Which still isn't on a professional level), but also because I've studied pedagogy at the University. I guess the thought that was a good combination. I also like to think that my extremely good looks had something to do with it... =). Anyway. It's great fun, and I hope I can continue to work there during my time at the University.
2. I've started writing a Database-sync engine for iOS together with a friend (Who's writing the same engine to Android). This engine will keep a mysql db, and two local sqlite-databases (iOS, Android) in sync. It's coming along slowly but it's great fun too build something this advanced.
Now I want to share some snippets of code with you.
In the sync-engine I've chosen not to use NSOperationQueue, but instead do all my threading with blocks and GCD. This is a great method for that purpose:
- (void)performOnNetworkThreadBlock:(NSDictionary * (^)())block thenOnMyThreadBlock:(void (^)(NSDictionary *))block2
{
dispatch_queue_t callerThread = dispatch_get_current_queue();
dispatch_queue_t networkThread = dispatch_queue_create("NetworkThread", NULL);
dispatch_async(networkThread, ^{
NSDictionary *dictWithResponse = block();
dispatch_async(callerThread, ^{
block2(dictWithResponse);
});
});
dispatch_release(networkThread);
}
You call this method this way:
[self performOnNetworkThreadBlock:^{
/*Do everything that you want to be threaded but it has to return a dictionary. For me this will be the response from the server.*/
//Example:
return [self doNetWorkCallWithDictAndReturnResponse:dictWithRequest];
}thenOnMyThreadBlock:^(NSDictionary *dict){
/*This block executes on the thread you called the method from. But will execute first when the network call is finished and will receive the response from the server that we returned in the previous block.*/
if (dict)
{
NSLog(@"Response from server:%@", dict);
//If we called the method from the main thread, it's safe to use UIKit here:
label.text = [dict valueForKey:@"title"];
}
}];
This is a really useful method, and it makes you see the power of blocks in Objective C.
This one is also really cool, but a bit dangerous:
This one is also really cool, but a bit dangerous:
When someone want to use the sync-engine to sync something, they provide the engine with a block of code they want to be executed when the sync has completed:
- (void)synchronizeEntity:(NSString *)entityName andPerformBlock:(void (^)(bool))block
{
self.callersblock = block;
callersThread = dispatch_get_current_queue();
// [self uploadAllNewEntitesWithEntityName:entityName];
[self buildCrossSyncDictionaryFromEntityName:entityName];
}
as you can see, the method saves both the block that the user wanted to execute after sync, and the thread the user called the method from in iVars.
they are declared like this:
//The block
typedef void (^CallersBlockType)(bool);
//The thread:
dispatch_queue_t callersThread;
The sync does a lot of things after this. But at the point of completion, or anytime else, I can just do this:
dispatch_async(callersThread, ^{
//Boolean value depending on result of sync.
self.callersblock(true);
});
that's really powerful. But watch out and remember that the user might reference to iVars and local variables in the block. So this should not be executed at an to late point.
Hope you liked those tips!
//Objective Coder