Google Depot Tools bug on iOS

Hope it will save some time for you. If you see

AssertionError: Multiple codesigning identities for identity: iPhone Developer

and don’t know how to fix, when building webrtc, libyuv or any other libraries with Google’s depot tools, simplest fix you can do is just remove the assertion. You don’t need developer’s profile and binary singing for libraries. That’s obvious for you, but not obvious for the script. 🙂 In my case, I found the assertion in the /chromium/src/tools/gyp/pylib/gyp/xcode_emulation.py.

P.S. Found similar post on Stackoverflow.

Javascript’s this is nuts

It’s not going to be a rant as title might suggest you. I’ve been reading a book about Phonegap (now known as Apache Cordova), and came across a cool remark in the text:

Javascript’s this is a bit
nuts, so it’s always better to refer to a variable that we know
will always refer to the object.

I smiled. This remark was referring to a simple code that had this assignment:

self = this

I’ve learned about JS “this” the hard way. Indeed, it’s not intuitive and goes against what developer learned from other languages. Anyway, I thought I know how it works well… until I read this quote. I went back online to refresh my knowledge, just to learn the damn thing again. 🙂 Anyway, in case if you’re in the same boat, old friend MDN (Mozilla Developer Network) is your friend. That’s the best article about “this” scope, strict and non-strict mode (who came up with these names!?), syntax for event handlers etc.

Here’s the link

asm.js

Idea of using Javascript as an intermediate for different programming languages is not new. I can say exact same thing about attempts to make browser runtimes closer to hardware. There was a big push toward JVM and Java in general at one point. Then Google did a splash with NaCl (which stand for Native Client). Developers needed a high performance secure environment to build efficient browser apps and have a path of porting existing apps from C/C++ realm to Javascript. asm.js supposedly is going to solve this issue by introducing a Javascript library and cross-complier from other high level languages into asm.js.
And this attempt is more serious than previous ones. For starters, it’s supported by Javascript godfather Brandon Eich.
Here’s his interview on this matter:
https://medium.com/javascript-scene/why-we-need-webassembly-an-interview-with-brendan-eich-7fb2a60b0723

From one side, this does not sound like a best solution for porting high performance apps like games (more exact, game engines), but since W3C will never come to agreement on lower level solutions, asm.js AKA Web Assembly may be the best compromise of the moment. It’s going to be based on many hacks and quirks. For instance, they virtually are going to disable GC. It did not take 20 years for web developers to understand that GC is a… garbage that they have to deal when it comes to speed optimization. There will be hacks around offline downloadable binaries or something like that. Otherwise, loading big games will be going to be a major pain. Anyway, it looks like major game engine developers are on board. Unity and Unreal already got prototypes and whole thing looks more promising than NaCl initiative.

Here’s another good article about Web Assembly:
http://ejohn.org/blog/asmjs-javascript-compile-target/

Unreal Engine is free now!

images

Epic Megagames decided to make Unreal Engine free. Good news!
Here’s the announcement and video message from Tim Sweeney:

Nice SWIFT tutorial from AirPair

In case if you want to get a basic introduction on what SWIFT is all about, look here:

Radio Shack TRS-80

Radio Shack is definitely dying, but its legacy will not die for sure. 🙂 Exactly 27 years ago in August of 1977 Radio Shack started sales of the first real home computer.
It did not have much of software at launch, had poor video text-only video controller, small amount of RAM (4Kb), crippled built-in Basic, crappy video monitor that could be purchased together with the bundle, but it was good enough to break sales records and create new market of home computers. Couple years later, Radio Shack could brag about having the largest available software library on the market, thanks to enthusiasts and young software company that were born together with birth of Apple II, Commodore PET and Radio Shack TRS-80, – three most popular mass produced computers of that time, together called “Trinity” by Byte magazine.

TRS-80

Authentication API for Ruby on Rails (Devise)

Devise is a commonly used authentication gem for Rails projects. It comes with controllers, data model and html templates that don’t require much time for configuration. Basically it works just out of the box for web projects. However, to my surprise, Devise is not that friendly with non-web clients. Say, you’ve built a REST API that works with your web app and now you want to reuse it with mobile native apps. Sounds reasonable and easy to do at first glance, isn’t it? Well, it’s not that easy with Devise.
If you try to google solutions for this matter, most likely you’ll stumble upon articles, talking about token authentication approach, similar to this one:
http://matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/
Unfortunately, this solution does not work anymore, because Devise changed.
It’s still possible to use token authentication, but you have to modify the code of your Session controller and Devise itself. I found that using token is not necessary as authentication cookies were working too. I did not need to change the Devise and append every single URL with ?auth_token=.
In one of the Devise own examples called “Simple Token Authentication” you can find following message:

Note: these examples are out of date, TokenAuthenticatable has been removed from Devise. See this gist for alternatives.

Here’s the gist it’s referencing to:
https://gist.github.com/josevalim/fb706b1e933ef01e4fb6

New article on how to build an API with Devise authentication:
http://www.soryy.com/ruby/api/rails/authentication/2014/03/16/apis-with-devise.html

Good article with the example that returns authentication token and result of the authentication back to the client:
http://jessewolgamott.com/blog/2012/01/19/the-one-with-a-json-api-login-using-devise

Basically, in order to make token authentication work again, you need to merge modifications for ApplicationController from gist I mentioned above and SessionController the way it’s shown in the last article.

My SessionController looks like this:

class SessionsController < Devise::SessionsController
  skip_before_filter :verify_authenticity_token

  def create
    respond_to do |format|
      format.html do
        params[:user].merge!(remember_me: 1)
        super
      end
      format.json do
        resource = User.find_for_database_authentication(:email => params[:email])
        return invalid_login_attempt unless resource
        resource.ensure_authentication_token

        if resource.valid_password?(params[:password])
          sign_in(:user, resource)
          render :json => {:success => true, :auth_token => resource.authentication_token, :email => resource.email}
        else
          invalid_login_attempt
        end
      end
    end
  end

  def invalid_login_attempt
    render :json => {:success => false, :error => "invalid login"}
  end

end

C++ STL containers with custom hash function

That’s a bit unusual trip to side roads for me. I’ve been working with C++ for many years and considered myself pretty experienced with STL containers. I remembered (I thought) how to extend a class so. C++ is a language that does not forgive hiatuses. My approach to custom hash function was similar to what highest ranking answer in this stackoverflow answer is showing:

http://stackoverflow.com/questions/647967/how-to-extend-stdtr1hash-for-custom-types

First of all, it’s a bit outdated, because with introduction of C++11 tr1 namespace became obsolete. Secondly, to my shame, I was unable to make my example compile. So after some head scratching, I decided to go for even simpler solution. It may be not as beautiful as what you can find in C++ books, but it works. So here it is.

Our sample class looks like this:

struct Square
{
unsigned long long x;
unsigned long long y;

Square(unsigned long long _x, unsigned long long _y) : x(_x), y(_y) {};
};

To make class work with STL containers like map and set you need to implement a method returning a hash value of the object.

struct SquareHash
{
size_t operator() (const Square&amp; square) const
{
return square.y * 1000 + square.y;
}
};

Besides that you either need to implement a function that compares two values. In case of regular std::map or std::set it should be “<” and in case of unordered_map and unordered_set – “==”. You can do it by implementing an operator< or operator== for operands of your custom class, but I found it even clearer syntactically to implement a class:

struct SquareEqual
{
bool operator() (const Square&amp; lhs, const Square&amp; rhs) const
{
return (lhs.x == rhs.x) &amp;&amp; (lhs.y == rhs.y);
}
};

Just in case, look at unordered_map declaration:

template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash, // unordered_map::hasher
class Pred = equal_to, // unordered_map::key_equal
class Alloc = allocator < pair<const Key,T> >// unordered_map::allocator_type
class unordered_map;

That’s it! Now you can declare an unordered_map:

unordered_set <Square, SquareHash, SquareEqual> visitedSquares;

Most popular iOS project on github

It’s the gitignore! 😀

Here it is: https://github.com/github/gitignore/blob/master/Objective-C.gitignore

And it has 22 contributors and 4812 forks! Wow!

Memory Leaks in [UIImage imageNamed] (iOS)

Yep, if you see your app bailing out with out of memory, you probably want to check how you load images into UIImage instances. Apparently that problem existed since iOS SDK was released and has never been fixed. At least I could reproduce it back in 7.0.1. It’s not exactly a memory leak, though you may see app memory usage going up in Allocations and Leaks tool after you load large images.
Technically it’s not a bug. More like an oversight on Apple’s side. UIImage does caching for images. Problem is that memory cache may take too much memory and make app eventually crash in OUM situation. It’s also a time waster for people who are trying to catch memory leaks issues that don’t exist and, believe me, tools won’t help you much finding the issue.
So, if you dynamically load images with [UIImage imageNamed], try to replace it with non-caching alternative:

 

- (UIImage *)imageNamed:(NSString *)filename
 {
 NSString *path = [AppUtils pathForResource:filename];
 UIImage *img = [UIImage imageWithContentsOfFile:path];
 return img;
 }

 

Apple’s docs confirms that imageNamed method indeed uses an image caching:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

Unfortunately, Apple does not provide a mechanism to control how images cached, so you have no choice but use the code similar to the example I’ve written above. If you need a cache for your images, then… well, you need to implement your own cache. Image caching implementation is outside of the scope of this article I’ll probably get back to it later. 😉

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.