Saturday, April 2, 2016

IOS - Maintaining a Block in Memory

IOS - Maintaining a Block in Memory

In Objective-C extension/block specification - "All Blocks are constructed to be Objective-C objects regardless of whether the Objective-C runtime is operational in the program or not. ", and they have the same memory layout. This consists primarily of its captured variables. Specifically for blocks the actual code is not stored in the memory but in a different part of the memory(text) with the rest of the executable code on your app.
When a block is created, the data associated with it are stored in the stack, including variables in its scope that it has taken a snapshot of. (The stack is a temporary memory that, in the form of a stack frame for the currently executing C function, stores information needed for executing that function--function parameters, local variables, and control information.)
When objective-c block goes out of scope, this memory ‘in place’ is subject to reallocation and then the block will be no longer valid.
If a block is used in the parameter of a method that is synchronous--that is, the code calling the block waits for the method to perform its computations before going on--there is no problem. The block will continue to be in scope, and its stack frame will still be there and available to the method being called.
If the method is asynchronous, however, the call of the method only starts its computation, which continues after the return of the method--there is a potential problem. The memory for the block may be overwritten before the code in the block executes. To avoid this problem it is common practice for an asynchronous method to copy the block from the stack to the heap by performing a copy operation. (The heap is the much larger and more permanent memory that contains segments of memory that the program can request.)
If it is otherwise desired to execute the block outside the scope it was created in, the block also needs to be copied to heap memory.

So, now let's see a block copy:

A block can be copied by sending a copy message to the block.

    void (^simplestThiagoBlock) (void);
    simplestBlock = ^{
         NSLog(@"Hi, I'm a Thiago’s block.");
 
    };
    [simplestThiagoBlock copy];

Please, understand that copying the block to the heap makes it subject to reference counting. If manual reference counting is used, the block should be released when no longer needed. So, keep this in mind in future.
In general, it is rare that you will need to copy a block. The first copy will move the block from the stack to the heap. Successive copies do nothing then only increasing the retain count. If you need to make an actual copy of a block, better you will need to recreate it.

Happy coding,
Thiago Leoncio.

Sunday, March 27, 2016

IONIC - How to get started with Ionic Framework

Hello everyone,

Today I am gonna show you how to start coding, emulating and having fun with this such great mobile app framework called Ionic. I've been using it 'for fun' since 2014 and it's an useful tool if you want to code as JavaScript instead of other native languages such as Objective-C and Java. In my case I play with both, so, it's really up to you whether or not use it. This weekend I am going to provide you more about it. I hope you enjoy it.

1- First have your Node.js installed on your computer. The current version working with Ionic is Node 4. Node 5, according to the Ionic site at this date, is not yet available for Ionic. Check the site related of Ionic here - http://ionicframework.com/docs/

I have the version 4.4.1 as you can see below on my Mac




2- Make sure you install or have it installed before you start the Apache Cordova Framework that works with Ionic. My current version of Cordova is 6.1.0 as you can see below.


3- I did install my Ionic 1.7.14 version.



4- Starting a project:
ionic start thiagoleoncioApp tabs

4.1- As you can see, it is going to ask if you want to register for push notifications or not.
I would recommend you say yes, as it helps you during your coding to have this option/feature available.


If you type y above, it will give you the option to register your notifications over Ionic site:


NOTE: You probably have noticed that I typed ionic start + my projectname+ tabs.



Here, in this start command we have option to start as blank,tabs or sidemenu. So, if you go with sidemenu your project will start with something similar to this.



5- Now you can run your Ionic project with these commands:
$ cd thiagoleoncioApp --> GO to your project folder created when you started it.
$ ionic platform add ios  --> Add in your platform IOS or Android or another mobile emulator you want to it.



$ ionic build ios

...



$ ionic emulate ios 

COMMON ISSUE: If you are running IOS emulator and you did not install yet iso-sim project you are probably gonna get an issue like this:

ios-sim was not found. Please download, build and install version 3.0.0 or greater from https://github.com/phonegap/ios-sim into your path. Or 'npm install -g ios-sim' using node.js: http://nodejs.org
Error: /Users/tguimaraes/thiagoleoncioApp/platforms/ios/cordova/run: Command failed with exit code 2


So, make sure you run the command - npm install -g ios-sim before emulate it in your Mac.

6- So now, emulating it:
...


7- Here it is your project structure created to develop in IOS using Cordova Ionic Framework.



 Just change the code or whatever you would like and move it on!





Next weekend, I am going to show you guys how to develop using Sencha Touch.

Another great article related of Ionic: How to set up Icons that displays differently amid iOS and Android devices

I hope it helps and happy coding!
Thiago Leoncio.