All posts by xuminjie

Update sqlite database during iOS app update

We all know updating sqlite database on an existing iOS app could be tricky, esp. with any schema (table) changes. In the good old days when I was developing in-house app, I found out the app would crash when I added a column to the sqlite database, after merely updating the app. The problem was, the old database won’t get updated if we don’t explicitly do it during app start-up (after app update). So what did I tell my user to do? Delete the app, and re-install the app. Not the most user-friendly way, and what if the user has some data on it that he/she wants to keep? What if this app have many users?

I had this updating issue in my mind for a while, because one of my apps needs to refresh its data. There are a few ways to do it: 1) Use a web service to get the data; 2) Just update the sqlite database. The latter is faster in terms of development. So I decided to do it. I did some research <a href=”http://stackoverflow.com/questions/2767849/how-to-update-sqlite-db-on-iphone-app-update”>on stackoverflow and found this post</a> as my reference. Here is my implementation, if you are interested. The following is done in viewDidLoad.

<code>
Database *dbAccess = [[Database alloc] init];

// do this instead…
// http://stackoverflow.com/questions/1601151/how-do-i-check-in-sqlite-whether-a-table-exists
// SELECT name FROM sqlite_master WHERE type=’table’ AND name=’version’;

BOOL doesVersionTableExist = [dbAccess doesVersionTableExist];
NSLog(@”doesVersionTableExist =%i”, doesVersionTableExist);

if (doesVersionTableExist == FALSE) {
// this only needs to be done once
BOOL createVersionTableSuccessful = [dbAccess createVersionTable];
NSLog(@”createVersionTableSuccessful =%i”, createVersionTableSuccessful);
}

// version =1.2.3; build =1.2.3
NSString *version_from_app = [[[NSBundle mainBundle] infoDictionary] objectForKey:@”CFBundleShortVersionString”];
NSString *build_from_app = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
NSLog(@”version_from_app =%@; build_from_app =%@”, version_from_app, build_from_app);

NSString *version_from_database;
NSString *build_from_database;
doesVersionTableExist = [dbAccess doesVersionTableExist];
NSLog(@”doesVersionTableExist =%i”, doesVersionTableExist);
if (doesVersionTableExist == TRUE )
{
// check buildNumber and version, theoritically we should update it after updating the database
NSMutableArray *buildNumberVersionArray = [dbAccess getBuildNumerAndVersion];
if (buildNumberVersionArray != NULL && [buildNumberVersionArray count]>0) {
BuildNumberVersion *buildNumberVersion = [buildNumberVersionArray objectAtIndex:0];
version_from_database = buildNumberVersion.version;
build_from_database = buildNumberVersion.buildNumber;
}

if ([build_from_database isEqualToString:@”1.2.4″] == FALSE || [version_from_database isEqualToString:@”1.2.4″] == FALSE) {
[self update1For2014:dbAccess];

NSLog(@”update build number and version: update1For2014″);
// insert or update, if we can it everytime during startup.
[dbAccess updateVersion:version_from_app withBuildNumber:build_from_app];
}

[self doWeNeedToRunUpdate2For2014:build_from_app version_from_app:version_from_app dbAccess:dbAccess version_from_database:version_from_database build_from_database:build_from_database];
}

// now I can use the refreshed database and tables

// Close the database because we are finished with it
[dbAccess closeDatabase];
</code>

stlplace.com was hacked and restored, plus some thoughts

I found out this site was hacked yesterday evening, as I saw the loading of site on Safari was slower than usual. It also shows incorrect theme, more like a plain theme. With the admin link redirect to spammer site. I decided to tackle it right away. I recall about one year ago something similar happened to this site, and google webmaster tool told me about it. I was able to remove the offending files/directories, by following the recommendation set out by google and some other wordpress sites.

This time I made an almost fatal mistake, during deleting some of the files in wp-includes, I accidentally deleted all the useful php files there. Panicked, I used both the website restore tool, and the import feature of wordpress (mojo marketplace), to no avail. The symptom of the problem was I could not login, and I can only see the pages at uudaddy.com (I could not log in there either).

I filed for help at the hosting company. But I still feel helpless as this site has about 9 years of my blogging and uudaddy.com has my last 4 years of blogs. Fortunately I was able to find this wordpress help page about updating wordpress, and fix internal server error by deactivating the plug-ins. The latter comes only after I gained some web dev experience lately, knowing more about error 500 🙂

So long story short, I was able to restore all the blogs (those two plus my wife’s happy mandarin.com) by the following:
1) Restore .htaccess file to avoid the redirect to spammer page;
2) Restoring the wp-includes and wp-admin page: upload zip file, extract; in the wp-content directory, I renamed plugin directory as plugin.SAV (this way it deactivates all the plugins);
3) Run the wordpress update as soon as I can log in.

I also backed up the MySQL databases for stlplace and uudaddy, and backed up those two blogs to wordpress.com (this one and this one). I understand blogging itself is a declining trend, and it’s probably not easy for small web hosting company to defend against hackers like Google/Amazon/Wordpress do. That’s why I am also evaluating whether to move to wordpress.com or Amazon EC2 (self hosting). It’s a bit emotional decision as I have hosted this site for 9 years, but I need to consider both my time, the cost of hosting, etc. I will make a decision on this shortly. Meanwhile check out the new blogging sites I mentioned above, in which I will blog both about software development and raising kids.

Rockstar developer

I have thought about this topic for a long time. I remember seeing at one place that an exceptional developer can do work usually done by 10 developers. Or in other words, he/she is a rockstar developer.

Besides productivity, I think rockstar developer has the following traits:

1) Willing to share the knowledge with fellow developers, keep in mind we all learn from each other, rockstar can learn from (dare I say) ordinary developer too;

2) Attention to the details and code quality, and other good development practice such as TDD (test driven development), again sharing knowledge is applicable here;

Continue reading Rockstar developer

Developer EQ series : 1

Dont’s
Beat up people in code review or interview;

Change other developers’ code without proper communication, or reasoning; worse, making the working code no longer work (sometimes to satisfying the “tests” 🙂

Do’s
Learn, learn something everyday;

Acknowledge our own limitations: acknowledge our mistakes if applicable, we all do make mistakes;

In the same token, when we learn something from someone else, show our appreciation;

PS, also came across this piece “assertiveness from developers” by Jeff Atwood (@codinghorror), which I think is excellent. Essentially the point there is be a good developer is not to be a “nice person to everyone”. A good developer uses his/her best judgement, and speaks from his/her own mind, which includes saying something make other stakeholder uncomfortable (as long as it’s fair and honest statement).

And, slightly off topic, this one “Your Company Is Not A Family” from HBR (Harvard Business Review).

Swift might the the language of the future

Apple : the Swift language (unveiled in WWDC on June 2, 2014)

Tutorials
Developing iOS Apps Using Swift Tutorial Part 1 (Jameson Quave)

learnswift.tips

References and reviews
Matt Galloway : Swift Language Highlights: An Objective-C Developer’s Perspective (raywenderlich.com)

The Verge : The Swift effect: Apple’s new programming language means way more iPhone developers and apps

Ray Wenderlich: Swift Cheat Sheet and Quick Reference (raywenderlich.com)

arstechnica.com : A fast look at Swift, Apple’s new programming language

Btw, Jeff Lamarche (twitter: @jeff_lamarche), the author of Beginning iPhone/iOS Development book, already got book offers for the Swift language. At the same time, don’t throw away Objective-C yet (HuffintonPost).

photo-3

Some iTunes App store tips

Avoid the Payload validation error
To shorten the review waiting time. I noticed this recently in my own apps update. The one without warning has much shorter review waiting time. The rationale is App thinks the app may use private API in this scenario (see this explanation on stackoverflow). From personal experience the one with warnings could be in the “wait for review” queue at least one week longer than the one without warning. So clean up the code if we can. Btw, I was able to fix the first one below (it turns out to be a third party function I no longer use). For the second one below, I read it could be related to Facebook SDK (which my app uses), but have NOT been able to solve it for now.

collegeFund_submit_warnings

ToMarket_submit_upload_warn

Validation
Validate before submit to App store, in Xcode organizer.

Keywords
Try put in relevant key words for the app during update (the only time we can change it); we can find some useful keyword by search App store. Also keep in mind search does not work all the time on iPhone. Code Signing
This one is very subtle. I had two certificates, one was used both for dev and distribution; another one was for distribution (Ad hoc or App store). I found initially I could not sign the app by using the distribution certificate (provisioning profile) alone. After back and forth I was able to sign. And as I look at the certificate in Xcode now, it’s refers to the Ad hoc certificate, but in the submission process it still prompts the first one.

    Ads or no ads

    Should I put iAd to my iOS apps on iTunes App store? Should I put the google adsense back to my blog? The reason I am asking this question is: while personally I really don’t like the ads on Facebook, LinkedIn, or the iAds on some apps like Echofon, I understand this is a meaningful way for app developers to make some money. This applies to me as well. While I did not spend whole a lot time working on my apps (in aggregate), I cannot go on this app dev road without any meaningful downloads/income revenue forever. I can do it for a while. But not for too long. I believe Ray Wenderlich talked about the importance of Ad revenue at recent raywenderlich.com podcast. One of the episode of Paul Kemp’s the App Guy Podcast (onemob.com) , talked about something similar.

    I guess, the trick is to balance the ads and app. We don’t want ads to totally destroy the good experience of an app, but at the same time, if the app revenue could help the developer keep going, go for it. We live in a commercial world, no money, no bread. No bread, how can app developers survive?

    photo

    Btw, I found the Apple developer documentation for iAd is insufficient or not up-to-date. For example, it uses requiredContentSizeIdentifiers in the tutorial, which is already deprecated in iOS 6. I found it won’t compile with iOS 7 SDK. I have seen in the past Apple does not always updates its developer documentation. Maybe they have not given high priority to this. I am sure if they pay attention, they can fix it.

      Singleton pattern backfired on me

      This is a continuation of my earlier post of Tree, recursive function and my dumb mistake. As I said in my last post, my solution was to create hash maps (singleton) to store those for the session. The main motivation was to help out the performance. But I found out it backfired on me, a few days ago and today. In both cases, it has to do with the hash map I created, it was something like (object , a list of objects). In both cases the list of objects was actually dynamic, and I made an assumption it was static. In other words, I thought I only need to put the mapping to the map once, then I can use it happily ever after. I think there might be ways to get around it, i.e., I should update the map when that list does change. I will investigate and work on it more. (Update 05-30-14) So I found a solution to one scenario, I was able to update the map as needed. And I use put method (java hashmap) to do that update.

        What types of developer are you?

        Or am I? 🙂 I think I’m a pragmatic programmer. Note this is also a book title I read, by pragmatic (note not agile), I think it’s about balance between software quality, effort and delivery date. It’s also about releasing software with known risks (including bugs) 😦

        Pragmatic_Programmer

        I thought about this as I started practicing some new (new to me) programming techniques, e.g., pair programming, test driven development (TDD), and also seen developers of different personalities/experience adjusting to this, at the same time trying to deliver the project on schedule. I felt and considered myself to be lucky in this aspect, as I thought my career at UGS/Siemens PLM Software laid the foundation for me as developer, in other words, I was very lucky to work with some excellent developers (definitely top 20% according to Jeff Atwood, co-founder of stack overflow), and received their guidance and mentoring in my programming formative years. Note I was not doing Agile, TDD, pair programming at that time, at least not formally. But at the latter part of my stay there, I did quite a bit automated unit/regression tests there. Also, although there is no pair programming, whenever I feel stuck, I could go to a senior member of the team, and talk. Not to mention the long debug session. I wrote about them in my blog long time ago (in Chinese, such as this one and this one).

        Continue reading What types of developer are you?