Categories
iPhone app

Create a timer on 2nd thread using Grand Central Dispatch GCD

Reading Time: 2 minutes

Last Updated on June 24, 2011 by stlplace

(Update 06-24-11) Last night I found “More iPhone 3 development” (by Dave mark and Jeff Lamarche) chapter 14 deveoted full chapter to multithread including timer.

(Original) As of iOS 4, Apple made blocks and Grand Central Dispatch (GCD) available. Previously it was available on Mac.

Some pre-requisite or recommend readings
WWDC 2010 video session 206: Introduction to Blocks and Grand Central Dispatch GCD (you need to be a registered Apple developer to download this).

Or if you read the nice intro “guide to blocks and grand central dispatch by Cocoa Samurai (Colin Wheeler).

The problem I attempt to solve
I have couple buttons waiting for the user to touch, and the buttons can not wait there forever, they wait for 2 seconds if a user does not touch any of them, and a new set of button will display at that time.

I thought about this problem for a while, thought about using NSNotifications which is essentially a callback mechanism (see “love to be notified” by Cocoanetics for more info). I need more, essentially I need a second thread which is a timer, to notify the main thread (which does display and handles touch) when 2 seconds is up.

I found fieryrobot’s Watchdog timer in GCD which meets my basic need for timer. But I need to return from timer to the main thread. Quote Fieryrobot:

…If you needed to return results to your main thread, you can just use dispatch_async to execute the code on the main queue (dispatch_get_main_queue() as we’ve seen in previous posts…

I am sure there is ways using blocks (callbacks) to do that, but I don’t know on top of my head. So I used Notification instead. Here is what I did in ViewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(twoSecondsNotification:)
name:@"TwoSecondsReached"
object:nil];

In the timer, I post notification when it’s time to go back to main queue (thread).
// Hey, let's actually do something when the timer fires!
dispatch_source_set_event_handler(_timer, ^{

NSLog(@"WATCHDOG: task took longer than %f seconds",
timeout);
// ensure we never fire again
dispatch_source_cancel(_timer);

// pass control back to main queue
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"TwoSecondsReached" object:nil userInfo:nil];
});

last but not least, the “TwoSecondsReached” function
- (void)twoSecondsNotification:(NSNotification *) notification
{
// clean up the timer here
if(timer != nil)
{
[timer invalidate];
[timer release];
}

// do things you want to do here.
}

One more thing
In my code, I initialize timer and release timer in different methods, and I found it’s easy to lose tack of timers and have multiple timers. One phenomena I saw is multiple timers fire at the same time, and make the app go crazy. I am thinking use a singleton pattern for the timer at this time.

Categories
finance

Some questions about 529 plan

Reading Time: 2 minutes

Last Updated on June 19, 2011 by omaha

I got those questions from a reader:
Why there is no national 529 plan? Looking at some states plan, it’s pretty much mutual funds like 401k, the investment return is like S&P? Why bother (consider S&P return is so low)?

I am not expert on this, I think why each state has a plan is pretty much why each employer (or most employers) has a 401k plan. The main purpose for such as plan is this: 1) Save some money for future liability (retirement or college tuition); 2) Hopefully the money can grow with the power of compound interest (snow ball effect); The tax deferral (in the case of 401k) or tax deduction (most 529 plans) are just ice on the cake.

As to why there is no national plan and federal tax deduction, I think this partially explained by college savings is still very small compared to retirement savings. Another indicator is the sales (or willingness for people to pay) my iPhone retirement savings/college savings app. Basically I gave away college savings calculator there because very few people are willing to pay 99 cents.

A side note: most people think spx (S&P index) is just boring, and some people think their retirement savings investments can grow 20% year over year. I think those people are too optimistic about their future. WSJ has an excellent article “The other midlife crisis” which explains why people expectations on income growth and retirement investment return are unrealistic.
Retirement income picture (via WSJ)

Categories
Business iPhone app

Going east: China iOS app dev companies and potential market

Reading Time: 3 minutes

Last Updated on May 10, 2014 by stlplace

(Update 05-10-2014) I saw the IPO of China based Cheetah Mobile in the news, not too impressed or confident of their apps though, although I know they make money. Cheetah was a subsidiary of Kingsoft (and King still has a big stake after IPO). I also noticed the WSJ article about China app market. I know a lot has changed since I last wrote about this, which is 3 years ago. From my personal observations, smartphone especially iPhone is saturated.

(Original 05-29-2011) I prepared the following summary for 360 iDev conference this fall at Denver. The talk did not make into the agenda, but I think it may be interesting to some western iOS (mobile) developers. After all, the market has great potentials.

Some background
iPhone officially came to China late 2009, but iPhone 1G came to China shortly after the US launch in Summer 2007. The first Apple store opened in Beijing July 2008. According to Steve Jobs Sept 2010 music event and Apple Q4 2010 earning report, greater China is the fastest growing market. Chinese costumers are snapping up iPod, iPhone, iPad and Macs quickly. How about apps? In this talk, we will discuss the China iOS app development companies (some have their apps featured by Apple), the current status of China iOS apps and the potential of iOS app markets (both consumer and enterprises).

Xujiahui

Some notable China based iOS development companies
139.me (Its Colorful Aquarium app was featured by Apple).
Rye Studio (featured article on San Jose Mercury News here).

Categories
iPhone app

Introducing myNestEgg v 1.5 and collegeFund 1.6

Reading Time: < 1 minute

Last Updated on May 28, 2011 by omaha

Latest and the greatest 🙂

1) Fixed “data not being saved” bug in myNestEgg and collegeFund

2) Also, in myNestEgg v 1.5, we added “Auto Pilot” feature, similar to what has been introduced in collegeFund 1.5. The main difference is in myNestEgg I set income ratio to 70%.

Why 70% for income ratio in retirement
I briefly mentioned it on my personal blog, 70% is a magic number because I assume social security and other savings made up the rest (30%) of the retirement income.

Categories
iPhone app

A few iOS gotchas: NSDate copy, sleep and multitask

Reading Time: 2 minutes

Last Updated on May 30, 2011 by stlplace

I was working on a new app and this app has something to do with timing. Basically I need to record the time between a button being displayed and the time a user touches the button (note they are in two different functions).

NSDate and memory leak
To accomplish this, I am using NSDate class, I declared NSDate * start in the class,
and then in one function, I was doing
start = [NSDate date];

and in the second function, I was doing
NSDate *methodFinish = [NSDate date];
NSTimeInterval intervalTime = [methodFinish timeIntervalSinceDate:start];

But the code above crashes immediately. After some research I believe this has something to do with memory, if we copy (retain) the NSDate as suggested by this thread in iphonedevsdk.com. It worked.

NSDate* now = [NSDate date]; // unretained
start = [now copy]; // retained

Sleep

Categories
iPhone app

Distribute Ad Hoc Application over the air OTA: some tips

Reading Time: < 1 minute

Last Updated on August 29, 2014 by stlplace

(Update 08-29-2014) As of iOS 7, this functionality needs some tweak. See this thread on stack overflow and this post. The gist of the change is Apple disabled http, it needs https now.

(Original 05-14-2011) I was following John Muchow’s tutorial “Distribute Ad Hoc Applications over the air OTA“. It worked as expected until the last step, when I click the link on my iPad 1, it says “can not download the app…”.

Did google on this, found one relevant discussion on stackoverflow. I was using my web hosting company web interface uploading the file, so I switched to ftp, changed to binary mode. Same results.

I realized my iPad 1 was still at iOS 4.2.1, while my Xcode was building 4.3 target. So I changed the target to 4.2 (in Xcode => Project Setting => build target), and repeated relevant steps in the tutorial (mainly to create the .ipa and .plist). Things worked after I made the change.

Btw, I found Xcode 3.2.6 build-in capability to share and distribute (submit to app store) is cool.
Xcode build and archive

Categories
iPhone app

Apple iTunes Connect Financial Report

Reading Time: 2 minutes

Last Updated on May 12, 2011 by stlplace

If you are iOS developer who has sold app (or ads, in-app purchase) on iTunes App store, the iTunes connect Financial Report is no stranger to you. Personally I published my 1st paid app to the App store last Sept, and I have since received financial report email notifications, and direct deposit. I did not pay much attention to the dates because the earnings are small amounts. I do understand Apple does things in its own fiscal calendar (developer can get it after log into iTunes Connect).

That is until recently, I found an interesting phenomena or glitch if you well. It took me a while to figure out the answer. Basically as of today I have not received the financial report for April, while I received the March earnings report on March 31. So I went to the “Payments and Financial Reports” section after log in iTunes Connect, and click the “Owed” tab, I noticed I received February Financial Report on March 18, and January report on Feb 18. See the patten here? Apple says it usually releases report 30 days after the month closes. Quote Apple (I got this answer from “Contact US”, then select appropriate topic:)

When/how will I receive financial reports?
Financial reports are distributed once a month and are available for download exclusively through the Payments and Financial Reports module of iTunes Connect. Financial reports are generally available for a given month by the end of the following month.

So this confirms my theory: Apple releases March report earlier than usual, which leads me to think it would release April report shortly after month closes. The usual time frame is around May 18.

Related question: when will I get paid?
Apple is quite consistent on this, it appears to me they do direct deposit 15 days after issuing financial report. This of course assumes:
1) A developer has set up the bank and tax information, agree with the contract (aka paper work);
2) Meets Apple’s threshold of $150 (it appears to me this rule is not strictly enforced).

Bonus question: why Apple release March report earlier?
hint: think about its fiscal calendar again 🙂

Categories
iPhone app

collegeFund v 1.5 is coming

Reading Time: < 1 minute

Last Updated on May 9, 2011 by omaha

(Update 05-13-2011) Released. Free for a limited time. Here is a link to the iTunes App store.

(Original) This is to address one problem posed by a user in the iTunes app store review. Basically his (her) comment is that the iteration approach (try and error to get desired coverage ratio) takes too much work.

I think the comment is a good one, and here I am making the “auto pilot” option available (the default is OFF). It works like this, in “setting”, the user turns on the “auto pilot” switch. In the “calculator” tab, when the user hits “calculate”, the user will have 80% coverage ratio, and the “saving annually” text field will have a value that achieves that 80% goal.

Categories
iPhone app

Top 10 questions asked by new iOS developers: I

Reading Time: 2 minutes

Last Updated on May 29, 2011 by stlplace

Top 10 misconception or mysteries facing new iOS developers. Summarized from my observations and own experience. Part 1.

Free app or paid app?
This is a big question. Apple iTunes app store is not a freebie store (compared to many other app stores), that being said, most downloaded apps are either free or cost 99 cents. It’s hard to make a living on 99 cents apps and at the same time providing free upgrades. Apple does provide other revenue models such as Ads (iAd and other ads) and in-app purchase. I have not tried those, and I think ads are good for app with a lot of downloads (at least 10,000, or 50,000), and in-app purchase are good for certain app or features.

Ads or No Ads?
I remember Mike Lee, a pioneer in iOS app development, once said don’t annoy users by Ads. That being said, we all need the money to live, again I think if we can make some Ads money without really annoying users. That’s ok.

Universal App or separate iPhone/iPad apps?
Tough question. Again it depends. I remember Kirby Turner said a few things on this topic at VTM last fall. His suggestion is do it when “functionality is same or similar for iPhone/iPad apps”, and “enhanced user experience”, on the other hand not to do it when “Functionality differs greatly”, and “business justifications”.

Categories
iPhone app Software development Web

Software updates

Reading Time: 2 minutes

Last Updated on April 12, 2011 by stlplace

WordPress 3.1.1
Finally I got hands around my blog (this one stlplace) and did the WordPress upgrade. I put off the upgrade in the past primarily due to my laziness: there is no automatic update due to I was running on WP 2.0.2. The upgrade itself is not too bad, I followed those two articles on Backup database and Run manual update. I used phpMyAdmin for the WP database backup. For the WP upgrade the only tricky part for me is:

“…Upload the individual files from the new wp-content folder to your existing wp-content folder, overwriting existing files. Do NOT delete your existing wp-content folder. Do NOT delete any files or folders in your existing wp-content directory (except for the one being overwritten by new files)…”

I know “directory overwriting” generally works on Windows system, but we are talking about UNIX (Linux to be precise). The command I used goes like this (in the blog root directory):

cp -r -u new_wp-content_dir .
(here -r means recursive for directories; -u means update)
I believe it did the trick, as I can run upgrade database afterwards. After that I enabled the necessary plugins. The only plugin stopped working is the ultimate warrior tags. The rest are fine.

Btw, I also added WPtouch plugin so that this blog works better on iPhone.

Xcode 3.2.6
I found my old Xcode 3.2.2 can not handle block as it’s necessary to run Ray’s RSS reader example. Download the software from Apple, install, and found this “Base SDK missing” in the new Xcode. Did some google, the trick is to “use latest SDK” in the “project => build” setting. I noticed we can also build “older” targets by changing the target in “deployment target”. Obviously there is a limit as to how far we can go back (I have not checked that yet).

Btw, I found the iPhone 4 simulator looks cooler than iPhone 3 simulator 🙂