Tuesday, November 13, 2012

When Magical Record stops being magical

Some time ago i discovered Magical Record. It offers some fantastic categories to help you setup your Core Data stack with a single line of code. But it's much more than a Core Data boilerplate thingy. It also offers a range of Active Record pattern like shorthand methods. For example you can find all the instances of a particular type of object without having to write your own NSPredicate and NSFetchRequest using MR_findAll.

Somewhat recently they moved to version 2.0, along with lots of others features they added support for Nested object contexts. If you haven't read about nested/ parent-child managed object context your missing out, especially if you've ever tried to use Core Data with multiple threads.

So when did it stop becoming magical?

Usually i use Magical Record to setup my core data stack, and take advantage of its singleton default context, for those times when i'm too lazy to pass it around like a good engineer. I've also been known to dabble a little with it's Active Record implementation.

However with 2.0, the developer(s) have chosen to change the internal structure slightly. A "Root" context is created at the top of the tree, this has a child context that is returned when you call the MR_defaultContext method. The root context controls the writing of data to disk. This means that if you attempt to save the MR_defaultContext your changes are not persisted to disk, but instead pushed up to the root context.

Well that awesome, right ...

Well it is, if it was obvious. Instead I was left no errors, an empty sqlite db, and no data. Personally I really think they should have just made the MR_defaultContext the root context, and allowed users to nest contexts themselves if they wish. In case your interested you can reach the 'Root' context by calling MR_rootSavingContext.

Anyways after digging around the source code, and a little RTFM I discovered that in order to actually persist your data to disk you have to call MR_saveNestedContexts. This method will recursively call up the tree and save each context. Neat, but not obvious, especially when the API convention is to simply call the native save:

So really this post exists to say, if your a fan of Magical Record and your pulling your hair wondering why save: has stopped working the answer is use MR_saveNestedContexts.

Thursday, October 18, 2012

Drawing UIImage's with blocks

I have a slight love affair with Quartz's drawing routines. I don't know what it is about them, but there is something I love about stroking rects and filling paths. It's like OpenGL for stupid people.

Anyways, during my love affair I got bit annoyed at having to subclass UIView every time i wanted to make a quick sprite, or overlay a one image on top of another.

CALayer's a are awesome and everything, but i still need to subclass UIView unless i want a bunch of layout mess every where.

So I thought wouldn't it be cool to be able to create UIImage's quickly and then just throw them into a CALayers or UIImageView's and worry about the rest later.
The above does just that, you give it a canvas size or more informally the size of the UIImage that you wish to generate.

It will create the CGBitmapContext with a RGB colorspace, and then you can draw to your hearts content.

You can treat the block almost like a you would drawRect. I say almost because it's not inserted into the graphics context stack, so some of the helper methods like [[UIColor blackColor] setFill] will not work, and you'll have to learn how to do it the 'proper way' (CGColorSetFillColor()). Just consider it a general rule, that if it renders something and doesn't take a CGContextRef you need an alternative method to use it inside this block.

Sunday, September 23, 2012

The evolution of lazy initialization

Lazy initialization is tent pole of Objective C. Apple placed a lot of emphasis on using the pattern in the early iPhone days. That was justified especially considering that the original iPhone shipped with 128MB of RAM, compared to the latest iPhone 5 with 1024MB of RAM.

In the beginning

The ARC evolution

The ARC evolution was fairly tame, allowing us to move the ivar into the implementation files. While the original method was great, and served me well until ARC. With no need to dealloc, and the ability to have iVars that aren't declared in the public interfac.

Clang takes it up a notch

With the most recent versions of Clang you no longer have to use the @synthesize & @dynamic keywords when making @property declarations.
This is all well and good, but I don't want to write my own ivars if I don't have to. So I've set about exploring ways that I can do away with having to create my own ivars.

Option 1.

This approach is comprised of three steps
  1. Public readonly property declaration
  2. Private readwrite property (re)declaration in a class extension
  3. Assign our instance to the ivar

The problem with this is that there is still the ability to overwrite the ivar. Granted it's only from within the class, but still not ideal. This could be an advantage as if you decide to move away from the pattern all you need to do is set the ivar via the mutator.

Option 2.

Another alternative, is to take advantage of another clang feature, where we use the @synthesize directive, to generate a backing ivar on request, without having to explicitly declare the ivar, or at least declare it in a conventional way.
Along with being the smallest version, we can specify the format of our private ivars, so if you want to suffix your underscore rather than prefix it, you have that option.

Whats the solution?

Quite frankly, i don't know. I'm leaning towards Option 2 as it doesn't provide an mutator for the ivar, and so there is almost no suggestion that the ivar should be mutated by anything thing other than the lazy accessor. However I'm interested to hear the opinions of others.

Thursday, September 20, 2012

I Took a Picture [Cambodia Edition]

A sunset in Sihanoukville, Cambodia


It feels like an eternity since it was announced but iOS 6 is finally available to the masses, and we can now talk about the all the (few) new goodies that it contains.
Apple included the Social Framework in iOS 6 as an abstraction layer for accessing the various social services that are now included with iOS. The key feature here is that you never have to deal with the Facebook SDK again if you wish...
So let's explore how you can use the new Social framework classes to replace REST API access functionality that is provided via the Official Facebook SDK
TL;DR; Sample code

Create an ACAccountStore

According to Apple's developer documentation the ACAccountStore
[ACAccountStore] provides an interface for accessing, manipulating, and storing accounts. To create and retrieve accounts from the Accounts database, you must create an ACAccountStore object. Each ACAccount object belongs to a single ACAccountStore object.
A simple alloc && init sent to ACAccountStore will suffice. Perhaps the easiest thing you'll do this week.

Get the Facebook ACAccountType

The 2nd easiest thing you'll do this week

Request access to currently logged in Facebook User

Before the callback block is called the User will be presented with a UI to confirm that they want your app to have permission to access this API. In the event that initial permission has been revoked, or disabled you will be given a negative boolean value.

Select an authenticated ACAccount

Your block will need to reference your ACAccountStore instance in order to see which accounts you have access to. To actually access the accounts you need to call the accountsWithAccountType: method which returns a NSArray of ACAccount objects. Where the type argument is account type we got in the previous section.
Apple has designed the ACAccountStore class to support multiple authorised user accounts. While iOS 6 only supports a single Facebook account, it does support multiple Twitter accounts. So while it is safe to assume that first (and only entry) in this collection is the ACAccount you want to use, the same cannot be said when you are working with Twitter accounts. Ideally you would want to present a 'picker' UI to the end user.

Construct a SLRequest

SLRequest is the Social frameworks NSURL request abstraction to enable it to support request signing, and token passing, features used by Twitter and Facebook. The SLRequest class also has a convenience method for dispatching this request.
If you are a fan of request queuing, and application wide request management there is also the option to extract a prepared NSURLRequest object with you can then pass to something like an AFNetworking request operation (See below).

That's it! If you weren't able to string the process together in your head magically, you can view the entire gist. Thanks for reading!

Tuesday, June 19, 2012

Parse + CocoaLumberJack = Remote logging

I love logs, they're just so useful. Usually when i want to do some logging i save the data to a CSV file using my FRCSVFormatter. However, i've been wanting to try out Parse for quite some time, so it was mash up time.

CocoaLumberJack is probably my favourite logging framework. Thanks to it's modular nature you can easily create your own log formatters, or in this case create your own loggers. What i've done is implement a Parse "objects"inside of a DDAbstractLogger subclass  which enables this ...

I've added in the [[UIDevice currentDevice] name] so that you can differentiate between the devices. You could also sort my messages, method name, file name and line number.

Check it out on Github

Wednesday, May 16, 2012

Around the sun and back again

One year on the road, and through almost zero planning i've ended up in the place that i've started, retracing a route that i previously travelled.

I coined a new phrase today, "My impressions of so much, are coloured by my knowledge of so little". It would appear that i have more to learn.

Sunday, April 29, 2012

一月在香港 ... One month in Hong Kong

Wan Chai & Central
Hong Kong is without doubt my favourite city on the planet. One of the things that i love the most about the place is how the buildings seem to twinkle at night, almost as if they are suggesting that they know something that you don't.

Something i had overlooked in my reading about the city was the cities architectural past. The Skyscraper laden strip of land that makes up the northern shore of Hong Kong island, used to be full of old Victorian buildings at the turn of the century. If you have an interest in colonial architecture, i highly recommend checking it out.

Three of Hong Kong's prominent buildings, (from left to Right) Bank Of China Tower, IFC 2, and the center.
I've always said i would love to live here, how realistic a possibility that is, i have no clue. But i'll certainly try to make it happen.

Sunday, April 15, 2012

Tiger Leaping Gorge (虎跳峡)

One of the last big things i did before i left china was to visit Tiger Leaping Gorge (虎跳峡). This had slipped off my agenda when i heard about the fantastic trekking opportunities that were to be had around the town Deqin (德钦), however i cannot explain in words ho cold it is 3600m up near the Tibetan border. So needless to say my time in Deqin was extremely brief.

The lack of a proper excursion in the foothills of Tibet meant that Tiger Leaping Gorge was back on the agenda. One of the reasons that this wasn't especially high on my agenda, is having trekked around Hawaii, and Australia by myself, i've found it really is more fun with company. Especially when you get into that classic "Do i go this way, or that way" situations.

My company came in the form of Peruvian woman that i met in Shangri-la (香格里拉) called Alessandra.  Together we walked the 18km from the upper gorge to the middle gorge. The whole trip took about 7-8 hours including a stop at one of the guesthouses half way to eat and pick Snickers bars (巧克力) for the trip.

The trail's configuration is a mixture of public roads, dirt trails, and steps hewed out solid rock. One of my favourite bits, and one of the most interesting is this waterfall (Image left). The gorge sits between two Gigantic mountains, both reaching well over 5000m at there respective peaks. As result melting snow running down the mountain(s) provides these amazing features. There is essentially no path at the waterfall, the water from the waterfall runs over the "designated" path and off the edge of the cliff.

While the water isn't a deep, it's not particularly dangerous (well as long as you don't lose your balance while taking HDR photos), it is an awesome experience to be perched between this falling fresh water and a 1000m drop down into the gorge.

So i have a disease, i'm an addict, a Computer addict. As a result of my disease i see it necessary to carry my Macbook everywhere me. Well in this case, i didn't feel comfortable leaving it at the starting Guest house, so i thought i really ought to bring it with me. The fact that i had a new episode of Family Guy, and had been doing quite well in my FM12 game did not influence my decision in any way shape or form.

Needless to say after a few kilometers it began to dawn on that i really needed to somehow make the fact that i was carrying a 3kg laptop with me 18km, worthwhile, The below was the result.

In many ways it actually sums up my trip quite well, travelling the world, glued to my laptop. Sometimes a consumer, others a contributor, but most importantly a creator.

But i digress, after the trek i was well and truely shattered. My legs hurt and while the pain did subside over the next few days it was still fairly taxing. Nothing that would have me advise people not to do the trek, but make sure you don't plan on doing much afterwards.

Anyways if your interested in seeing my route in additional detail, i've used my billion dollar GPXShare app to upload my GPS trace, which you can enjoy here.

To close, Tiger Leaping Gorge was impressive, the views amazing, however from the short amount of time i spent in Tibetan hinterland that was Deqin, and the feilai temple (飞来) i'm confident that even better trekking is to be had there.

Thursday, April 12, 2012

Dinner Diary - 馅饼(xian bing)

Sadly my second trip to China hasn't been filled with anywhere near the same amount of street food as i had hoped. Maybe 云南人 just don't like eating on the street, who knows. Having said that i found 馅饼, without much searching. It's very similar to the Baba that is found in northern Yunnan, however this is filled with pork mince and cooked in large amount of oil, so much that it's almost dangerous for health and clothing to eat it with your hands.

One xianbing is 6 yuan (£0.60)

Thursday, April 05, 2012

Dinner Diary - Naxi Sandwich

This one isn't a traditional recipe as far as i know but instead a western take on a authentic Chinese food. "Baba" is a traditional flat bread from Naxi people in Yunnan province. This sandwich is essentially two pieces of Baba with a fried egg, tomato, red onion & cucumber. It's fairly awesome, and has a really fresh taste. Another "fusion" dish was "Chocolate Baba", sadly this was only chocolate sauce on a Baba, not chocolate inside a baba (huge difference).

Anyways this Naxi sandwich came in at 16 Yuan (£1.60)

Thursday, March 29, 2012

Dinner Diary - Hanoi Mystery sweet

Vietnam is a good place to be if you like sweet things. Kem Flan, Ca phe sua de, and the is mystery food. It was possibly the cheapest thing i got in Vietnam, costing around 5000 Dong in back alley of Hanoi.

It was essentially like cornmeal porridge on a rice cracker with sugar sprinkled on top. Sounds basic but it was awesome, and extremely sweet.

Dinner Diary - 粑粑 (Baba)

Yum, bread. It's like it's been the theme of my time in South East Asia. the place i had always sworn that you could not get decent bread.

Baba apparently comes comes from the Naxi minority people in Yunnan province, China. Sometimes it's filled, sometimes it's just plain. The joy is in the discovery. I've had at least 4 different types of Baba in the same town. One with Red bean filling, one with a sweet jam like filling, a plain one, and a baked (rather than fried) version.

Baba on the streets on 大理 ranges from 2 yuan all the way to a mind shattering 5 yuan (daylight robbery if you ask me).

Dinner Diary - Dali Noodle Surprise

It seems to be almost customary now that i try random things in random countries. The above dish is one such example. It consisted of Rice noodles (米线), strips of pork, and the shop had a series of veggies and spices that you could add to it. I just threw in handful  of spring onions.

This dishy dish came in at 6 yuan (£0.60p).

I Took a Picture [Tibetan Edition]

Meli Snow Mountain, Tibet. Taken From Feilai Temple, Deqin, Yunnan

Sunday, March 04, 2012

Dinner Dairy - Bo Bit Tet

Since i've been in Vietnam, i've eaten a lot of food. But i think with Bo Bit Tet, i've found one of my favourites. This is especially unusual as there is very little Vietnamese about it. The dish is made of a salad, chips/ french fries, a bread roll and "steak". Beef in South East Asia is a far cry from the meat in Europe & the americas.

What makes this dish so good is the gravy/ sauce. Oh the sauce! It was so good i had to purchase another Banh mi (bread roll) to sop up all the sauce.

The Bo Bit Tet cost 60 000 Dong (US$3/£2) on the rainy, cold streets of Hanoi. Incase your interested the additional bread roll cost 3000 Dong (£0.10p).

Thursday, March 01, 2012

Dinner Diary - Bun Cha

Today's dish was Bun Cha. Unlike a couple of the recent postings i actually knew what i was getting into with this one. Having seen the signs up on the streets of Hanoi, i had done a small amount of research and knew what to expect. The soup is slightly sweet, and together with the noodles and veggies makes a great combination. Floating around in the soup are bits of BBQ pork wrapped in various leafs of one form or another. It's usually eaten by popping some noodles and veggies into your soup, and letting them soak up a little of the flavour, before popping the whole deal in your "pie hole". On the streets of Hanoi's Old Quarter, it cost 30000 Dong (US$1.50/£1)

Sunday, February 26, 2012

Dinner Diary - Vietnamese Train Surprise

So i'm sitting on a train somewhere between Hanoi and Da Nang when a train company employee passes by my hard sleeper hawking some stuff. Having seen a couple of other vietnamese people go for it, i thought why not. Inside the white casing which was nicely wrapped in a leaf, was a mince pork filling with a quails egg, Quite tasty. The unfortunate thing, is i have no clue what it is, but it was "cheap-ish" for train food coming in at 30000 Dong.

Friday, February 24, 2012

I Took a Picture [Central Vietnam Edition]

Hoi An, Vietnam, 3 Exposure HDR

Dinner Diary - Coconut Cake

Who loves Sugar ... i love sugar. As a result i was over the moon when i discovered this one. It's essentially sweetened grated coconut inside a bread like bun, deep-fried. Totally awesome, however a tad expensive, weighing in 10000 Dong (US$0.50) each.

Dinner Diary - Banh Xeo

Today's dish was Banh Xeo. This dish consists of three parts, a deep fried Pancake/omelette with bean sprouts, pork, and spring onions, a salad of lettece and mint and some other mysterious vegetable, and Rice paper. The sauce in the bottom right of the image above is fish sauce is a couple stray mint leafs. The glass used to contain Ca phe sua da (lit. coffee milk cold)

The lady at the restaurant (yeah, i didn't eat on the street, i was surprised too), informed me that the proper way to eat the dish was to tear off a piece of the pancake, add some salad, and roll it in the rice paper, and then dip it in the fish sauce (see below).

"They see me rolling"
Ready to go, not before dipping in fish sauce of course
 Once we've rolled up, we dip it in the fish sauce and enjoy. The portion size is surprising big, and the end result is a filling meal. In many ways it reminded me of my favourite dish so far in Asia, Roti Canai. The price however is astronomical in comparison, with Bahn Xeo costing 25000 Dong (US$1.25/£0.75), while the infamous Roti Canai could be had for as little as £0.20. However either way you cut it, it's good.

Wednesday, February 22, 2012

Dinner Diary - Vietnamese Market surprise

Yet another trip to a market, and yet more food that i have no clue about. This dish was contained  rice, tofu, shrimp, chicken, beansprouts, green beans, loads of fish sauce and other assorted veggies.

I think this was a slightly more "natural" dish, where it has no real name, but is just an assortment of good food thrown together on a plate.

Total cost 30 000 Dong (US$1.50/GB£1.00) including some vietnamese tea.

Monday, February 20, 2012

Dinner Diary - Pho Thịt lợn

Pho is possibly the most popular vietnamese dish abroad. Strangely the first time i had Pho was in Honolulu, Hawaii. I had this particular dish in Da Lat, it features some pork sausage and perhaps the lightest beef i've ever seen, however there is an extremely large possibility that this is pork.

This lovely dish set me back around 40000 Dong (US$2/ £1.20), along with the ever present ca pha sua da, which is Vietnamese ice coffee with milk. 

Friday, February 17, 2012

Dinner Diary - Meat Ball soup

One of the things i never appreciated about Vietnam is that it's really big. It also has a big population (over 90 Million). Because of the country's size there is a fairly big variation in the climate of certain regions. 

This was especially true in the town of Da Lat, located 2000m above sea level. As a result of the attitude the town is much cooler than most of Southern Vietnam, with temperatures hovering around the mid to low 20's in the shade.

In my experience, cooler climates usually equal awesome stews and soups, and Da Lat didn't disappoint. This lovely dish consisted of Noodles in a tomato-ish both, with a giant (bigger than my fist) meatball, as instructed by my vietnamese speaking travellers i threw in some cabbage and soy sauce to make the dish a little more interesting. Chilli's/ chilli sauce is also advised, however it was cold and i wanted to wolf it down.

If memory serves it set me back 20 000 Dong, which at time of writing was just under single US dollar.

Good Soup

Dinner Diary - Banh Mi

One of the fascinating things about travel for me is learning about how history has influenced the food of a particular county. The french colonisation of "Indo-China" has resulted in Cambodia and Vietnam being quite good at making bread. Something that is extremely unique in south-east asia. Generally the bread is awful, 5 months in i think i get to say that.

As i highlighted in a previous post, Cambodia has it's "Cambodian sandwich", Well not to be out done, Vietnam has it's own, the Banh Mi. From my limited understanding of Vietnamese, Bahn mi translates directly to "bread".  If you approach one of the vendors and just ask for a "Banh Mi" you'll be given a baguette. However if you ask for Banh Mi Ga, that is "Bread with chicken" then the fun starts. Ideally you should ask for everything ... i don't know how you say that in Vietnamese, however a circular motion near the various meats usually does the trick.

Banh Mi 'Everything'

Inside the everything option is usually beef, chicken, sweet chilli sauce, soy sauce, carrots, cabbage and more. They are quite easy to find in the south of Vietnam, (Ho Chi Min, Da Lat, Nha Trang etc), and can cost anywhere from 10 000(US$0.50c) to 25000(US$1.25) Dong.

Banh Mi Op La

But wait, there's more. Banh Mi Op la is another variation. What's the difference, simple it's egg inside the Banh mi. Usually these are fried "Sunny side up", and with the yoke still fluid so that you can enjoy some yellow goodness.

Friday, February 03, 2012

Acquiring a Chinese visa in Cambodia

In recent times the Chinese Immigration services have become more and more particular about the procedure required to get a chinese visa. My application was denied twice in Bangkok, Thailand for not having proper proof of my entry and exit into China. This was an issue as i want to travel into Southern China via Vietnam overland on a Train. And my Vietnamese isn't exactly conversational (read non-existent), so buying train tickets over the phone from bangkok isn't really an option.

However there is some light at the end of the tunnel. Turns out that the Chinese Embassy in Phnom Penh, Cambodia is a little bit of a rebel and doesn't ask for any of the aforementioned details. You turn up, fill out your form, and 3 days later you get the best sticker in the world. The other plus is that a single entry, 30 day Chinese visa in Bangkok costs around 1650 Baht for the regular service, that's about US$55 or £33 (Jan 2011), while in the "Kingdom of Wonder" (Cambodia's tourism tag line) it will cost US$30/£20.

On a side note, i highly recommend going to the Chinese Ambassador's residence in Phnom Penh, it's on street #380 at the bottom of street #51, like most things that involve the Chinese government its massive!

Friday, January 27, 2012

Dinner Diary - Beef Lok Lak

It's taken a while to while to find good cambodian food. Not because most of it's bad, but instead because they tend to only serve western dishes in most of the places easily accessible to tourists. This dish is very similar to Malaysia's Nasi Goreng, however the flavours are fairly different.

It's boiled rice, with a fried egg, beef, lettuce, green tomatoes, and the sauce on the side is lime/lemony thingy that is so totally awesome when combined with the rice. This dish set me back 7000 Riels, roughly £1.20.

Beef Lok Lak

Wednesday, January 18, 2012

Dinner Diary Cambodian Sandwich

I had heard a bit about this dish before I went in. Generally I avoid bread in South east Asia. However because the French influence in Cambodia you actually get really good bread, same is true of Vietnam apparently.
Anyways back to the sandwich, its fries chicken mince and bean sprouts, some other assorted veggies with spam to "garnish". The baguette is slightly toasted so it has a lovely crunch when you bite into it.
In word, awesome. The Sandwich and the black ice coffee(with a crap load of sugar) cost me about US$2.50/10000 riels/ £1.50.

Dinner Diary Cambodian Market surprise

Not for the first time but here is a dish for which I have no name, don't really know what was in it, but can say that it wasn't that bad.
Beneath the fried egg were some (flour) noodles mixed with some veggies, and what I believe to be a cassava dumpling with spinach inside. The dumpling was shallower fried so the texture of the dough wasn't consistent, some parts being a little mushy. Anyways all said and done this one was a $1.

Dinner Diary Pumpkin Potato curry

Had this lovely dish in Cambodia. Not really sure how Cambodian it is, but I can say with confidence that it was good. Came in at a whopping $2.

Monday, January 02, 2012

Dinner Dairy: No Name

Vegetable No name, it's essentially more of an appetizer than an actual meal. Just imagine a bunch of veggies, in batter, deep fried. Simple yet effective. Came in the grand old price of 50 Baht (£1)
Vegetable No Name