Thursday, December 15, 2011

ZipArchive

Yesterday's challenge was about to integrate a zip library to my project.  My goal was to download a zip file from the server onto my iPad and then extract it to the Documents folder of my project's sandbox, and then use the files inside it to display to the user. So, I decided to use this ZipArchive class from here. ZipArchive is a simple to use class which depends on the minizip library, and minizip classes are written in plain C. 


In order to integrate this, it's adviced that you put all the ZipArchive classes and the minizip classes to your project and then add libz.dylib framework to your project. However, when I do these steps in my project's source code, strange compiler errors about my NSZone and NSObject classes appeared, which I wasn't able to rationalize in any ways. Why do my iOS SDK classes get messed up when I try to compile some C code? 


Then to bring the mystery one level further, I tried to compile this zip library by putting them in a new XCode project. There everything goes nice and easy with no compile errors. So I couldn't understand this, but as always, I found a workaround :] 


I compiled the zip classes into a static library, in the new XCode project where everything is smooth. Then, added the header files of ZipArchive and the minizip classes to my project, along with the .a output of the static library. Then I had no compile errors in my project because the library was already compiled, yay!


Another thing I learned from this was: When I was compiling the zip API source code to get the static library, if the simulator was the target, then the library contains x86 code. And when you want to use this static library in your project that you want to compile for running on the device, the compiler gives an error saying that the static library was not built for this architecture and it's quite right. The vice versa is also true, if the device is the target while building the library, then it contains ARM code. And when you want to use this library to run your own code on the simulator, again the compiler doesn't like this.




So, the solution is to get a fat static library that contains both x86 and ARM code. This is how you get it: You compile your library once for the simulator and once for the device and now you have two .a files. Then you open the good old terminal and type:

lipo -create libdevice.a libsimulator.a -output libcombined.a
Now you can use the combined .a file whether for running on a device or the simulator.

Tuesday, December 13, 2011

UIView+Hierarchy

Today I came across a very handy category class, named UIView + Hierarchy. It provides some useful methods like below:
-(int)getSubviewIndex;
 
-(void)bringToFront;
-(void)sendToBack;
 
-(void)bringOneLevelUp;
-(void)sendOneLevelDown;
 
-(BOOL)isInFront;
-(BOOL)isAtBack;
 
-(void)swapDepthsWithView:(UIView*)swapView;

These aren't so hard to implement but having them in a category makes you have a cleaner code, since you encapsulate the hierarchy management in a category.
Here's a screenshot why I needed this: On the right hand side, there are two buttons (Program - Ziyaret Ozeti), that act like a folder cap. The active one should be one level up in the Z-order. When you deactivate it by selecting the other, then it should fall back one level and the other one should get in the front. Now it's so nice and easy :)
You can download the actual source from here

Saturday, December 10, 2011

Love your .pch files

Today is the day I got enlightened about the .pch files.  This is the prefix header for all of your source files in your project. If you have a utility class for example, that you want to import from all your sources, than you can import it in your .pch and then it gets imported from every class. Also if you like using macros for logging, and asserts, etc.. then you can also put them here. 


For example, I change the behavior of my logs and asserts according to my build configurations. To do this, I have a macro _NSLog that logs as usual if I'm in DEBUG configuration, and does nothing if I'm in other configurations. This lets me get a log free distribution package. I have the same logic for the ASSERTs.


This is the .pch file of my current project. BTW, you can see the integration with NSLogger here. When I call _NSLog from any class, LogMessageF of the utility gets called in DEBUG configuration and nothing happens in the others.  


Also see how I tag my logs by using _NSLogJSON, _NSLogDataMan, etc.. which in turn produces logs with tags "JSON" and "DataManager".



NSLogger, where were you all this time?

Today I integrated  a logging utility named NSLogger to my own iOS project. It has so many great features like letting you group your logs, view them easily in it's own viewer desktop application, log data and images, etc...And it's way too easy to get it working.


Another awesome thing that comes along by using this is that, when I hand in my iOS application to the testers who're in the same network with me, I can view the logs that their actions produce in my viewer app wuhuu!
Anyways, the github page of the project tells so much more, so go check it out:
https://github.com/fpillet/NSLogger



Monday, August 15, 2011

Adrenaline of the day

OK, back from the moon! 


Today I installed XCode 4.2 and iOS 5.0 Beta SDK to my machine but I didn't override the ones on my disk and I installed them on a different location. 
Then, when I was trying to automatically build and deploy the project with my ant script, the build kept failing. And here is the reason: When I installed the new SDK, this somehow changed my build settings and xcodebuild wanted to use 5.0 SDK even though in my project settings there's no mention of 5.0 SDK that I could see. 


I understood this by running xcodebuild -showsdks command from the terminal.  Then to fix this, I ran sudo xcode-select -switch /Developer,  which tells that the configurations under Developer directory is the master. Because this directory contains my development tools which I didn't want to override in the first place).
(Click on the image below for the the commands and the outputs. )




 Anyways, it was quite easy to fix and quite annoying at the same time. I'm writing it down in case someday, someone wants to document all the facts about how Apple is tormenting people all over the world.

Saturday, January 22, 2011

App pools on a saturday night

I needed to learn more about IIS 7.0 application pools since we're facing a problem about around 20 ASP.Net web applications residing in a single application pool and when we analyzed the IIS logs, it seems that one app is trying to be the smart ass by taking most of the resources. So, here is what I learned:

In IIS 7.0, you have the chance to group your apps into separate pools. By default, an application pool gets 1 worker process (w3wp.exe), but it's configurable. So, the default setting of one 1 worker process in an app pool means that all Applications/AppDomains in the pool share the same worker process. 

So, then comes the question: Should I put all of my applications in one app pool, or should I separate them to different app pools? What would be the pros and cons? 
Here are the facts that you should consider while making your decision:

  • App pools can be configured to use different identities. So, you can restrict one app of yours one way and your other app the other way. With app pools, you can also restrict an application access to resources in another pool. 
  • If you separate your applications to different app pools, then you can restart one app pool without affecting the other applications running in the other pools.
  • And this one is the that concerns me the most: If you have an app that is resource intensive, has memory leaks or misbehaving then you can place it in a separate app pool to ensure that it does not affect the other apps' performance.  Before IIS 7.0, if an app that is hosted under IIS crashed, it would also make its host process crash, too. So now with the app pools, you can at least keep your other good behaving apps safe and sound.
About the cons of dedicated app pools, since each app pool has its own bank of memory and its own process, it can use more system resources and there's a large memory overhead in running several w3wp.exe processes. But for me, it should be measured to see whether 20 apps in one pool use more memory then 20 apps in 20 pools, or not and then come to a conclusion. 

But anyways, maybe there should be nothing to rack your brain over on a saturday night. So, that should be all. Check these links if more is needed: [1] [2] [3]

Monday, January 3, 2011

iPon


What I learned today about iPhone apps is:
The software architecture of an iPhone is something like this from top to bottom:
  • Your sweet application
  • Cocoa Touch Framework: (Contains UIKit and Foundation framework) Handles UI elements, events, things about application lifecycle. Also contains wrappers around certain data types (like mutable/immutable arrays, sets, dictionaries), basic utilities and conventions for some core concepts like memory management, data management, etc..
  • Media: handles graphics, animation, sound and video
  • Core services: Data types, things like location awareness and networking, database access with SQLite, etc...
  • Core OS layer: OS services like I/O, threads, sockets, power management, etc...
Misc tips:
Method calls actually represent messages to the objects in memory. These messages are dispatched by the runtime. So this means that if you're calling a method on a null object, you don't get a null reference exception because for the runtime this is seen as a message is being sent to a nonexisting object, so the runtime does nothing when this case happens and no exceptions get thrown.

If you want to get the third character of your myString instance, then your method call looks like this: myChar = [myString characterAtIndex: 3] . Here the parameter is supplied with parametername: value construct, which is named as a "keyword". 

It's really hard to be both alienating and mysterious :D

Sunday, January 2, 2011

Mercurial with Kiln

Last month I wanted to give a try to Mercurial as a distributed version control system. While searching for an appropriate hosting option to work with Mercurial, I came across Kiln. Kiln is a product of Fog Creek Software and as a fan of Joel Spolsky, I wanted to use it with no doubts. And what's more is that if you're a student or if you have a team of two people or less, it's completely free for you, yay! 


After you sign up with Kiln, you choose your repository's url and then you can use the web management interface of Kiln + FogBugz. 


FogBugz web UI is for projects, cases, assignments, milestones and that kind of software management crap. Kiln web UI is for managing your repositories. FogBugz also has a wiki tool which can produce sleek looking wikis that you can publish publicly or privately. 


Besides these, you can download Kiln Client as your Mercurial client. Kiln Client extends Tortoise Hg and if you download it using your Kiln account, it will come pre-configured for you, so that you wouldn't have to bother with configuring your repository and so.  To download Kiln Client, you can use the Resources link at the right top of your kilnhg page.


And if you want to integrate your IDE with Mercurial, go ahead with the following links if you use VS or Eclipse. If you don't, go ahead and use VS or Eclipse, eheie :]
Visual Studio
Eclipse
.widget { margin-top: 30px; margin-bottom: 30px; }