Strings!!!

Anyone who have done some form of programming will know of this term, Strings!

Its one of the more commonly used in the programming environment.

Today, I am writing this to talk about performance of Strings in C#.

String strValue = “This is a String”;

Above is a simple string declaration.

Consider the following example:

String strValue = “This is a String”;
strValue += ” This is another String”;
strValue += ” This is the Third String”;

The above method in .NET will use more memory, reason being that += will create a new string and discard the previous ones to the garbage collector (this is something alot of developers in .net doesnt care about because .net deal with it).

Instead of the above, do the following:

1.
Stringbuilder builder = new Stringbuilder (“This is  a String”);
builder.append(” This is another string”);

2.
string newString = String.Format(“{0} {1}”, “This is a String”, strValue);

SQL : Maximum Recursion Limit

Recently at work, I got back in touch with dealing with Microsoft .NET technology.

Something that I haven’t working on for awhile, long story short the requirements was simple generate report to webpart (Sharepoint) using their custom application database.

Seems fairly simple, but with a fairly complex database schema, I adopted the view approach and leave all complex data manipulation to the SQL Server.

Until I hit the following error,

MAXIMUM RECUSION LIMIT 100 have been reach.

This normally means that you are not able to progress further, after much figuring with the problem. I have found 2 solutions.

1. Data Issue, in my case this was the problem, due to the quality of the  sample data. I had to generate my own data sample and it was all good.

2. You will have to add “option (maxrecusion 1000)” after your select statement.

A little bit more on this, the SQL Server maximum recursion is 32767.

By default this value is set as 100, therefore you can set the number anywhere from 0 to 32767.

Note:
To bypass this error, you can simply set this to ZERO, but this doesnt mean that the problem goes away.
The reason this error appears means that the sql query that was written have some issue and its best to find the root cause of it.

Standard Height and Size for iphone

UIView – 320x460UINavigationBar – 320×44
UITabBar – 320×49
UISearchBar – 320×44
UIToolBar – 320x44UIPickerView – 320×216
UIDatePicker – 320×216
UIKeyboard – 320x216UISegmentedControl – 320×44
UIButton xx37

UITextField – xx37
UISwitch 94×27

UIActivityIndicatorView – 37×37
UIPageControl – 38×36

UIKeyboard – Height : 170

UITableViewCell – 320×44

IOS GameKit

Implementing Bluetooth with GameKit

In this article, I will share how to enable Bluetooth with GameKit api.

First, we will have to add GameKit library to our project.
Select your project and the corresponding target.
Under “Build Phases”, expand “Link Binary with Libraries”. Click the “+” button and add the GameKit Library
When you successfully add the GameKit framework. It should appear as in Fig B.
Now that we’ve added the GameKit framework, we can move on to implementing the codes for Bluetooth connectivity.
We will now need to create a controller that will be implementing and using the Bluetooth.
In the header file, make sure you input the following codes

#import <UIKit/UIKit.h>

#import <GameKit/GameKit.h>

@interface BluetoothViewController : UIViewController<GKPeerPickerControllerDelegate,GKSessionDelegate>{

    GKSession *gkSession;

    GKPeerPickerController *gkPicker;

}

@property (nonatomicretainGKSession *gkSession;

@property (nonatomicretainGKPeerPickerController *gkPicker;

-(IBAction)connectViaBlueTooth:(id)sender;

@end

Now, we will move onto the implementation file.

@implementation ExchangeGKViewController

@synthesize gkSession, gkPicker;

- (void)viewDidLoad

{

    [super viewDidLoad];

    if (!gkSession) {

        gkSession = [[GKSession allocinitWithSessionID:nil displayName:nilsessionMode:GKSessionModePeer];

    }

    

}

With this, we will synthesize our session and picker so that we are able to use them.

We have also implemented a session to be created so that when we first load the viewcontroller, there will always be a valid session.

Now, we will create a button for the viewcontroller to look for other bluetooth peers.

Back to the header file, add in:

-(IBAction) connectViaBlueTooth:(id)sender;

-(IBAction) disconnectFromBlueTooth:(id) sender;


And implement it with :
-(IBAction)connectViaBlueTooth:(id)sender{}    gkPicker = [[GKPeerPickerController allocinit];    gkPicker.delegate = self;    gkPicker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;

    [gkPicker show];

}

-(IBActiondisconnectFromBlueTooth:(id) sender {

    [self.gkSession disconnectFromAllPeers];

    gkSession.available = NO;

    [gkSession setDataReceiveHandler:nil withContext:nil];

    gkSession.delegate = nil;

    gkSession = nil;

    

}

Don’t forget to create the buttons, and link it with the corresponding IBAction.
Go ahead and test what you have so far. If everything is working well so far you will be able to see
You’ll also be able to see other devices if they are looking for bluetooth devices too.
If you’ve gotten this far. Congrats. Pretty easy so far. Now for the finishing touches.
We will need to implement codes so that the PeerPicker knows what to do when you select a peer

#pragma mark - GKPeerPickerControllerDelegate methods

- (void)peerPickerController:(GKPeerPickerController *)picker

              didConnectPeer:(NSString *)peerID

                   toSession:(GKSession *) session {

    self.gkSession = session;

    session.delegate = self;

    [session setDataReceiveHandler:self withContext:nil];

    picker.delegate = nil;

    [picker dismiss];

}

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker

{

    picker.delegate = nil;

}

Add in these codes so you know the changes in Bluetooth connectivity state as well as to send data and receive data.

#pragma mark - Session methods

- (void)session:(GKSession *)session

           peer:(NSString *)peerID

 didChangeState:(GKPeerConnectionState)state {

    UIAlertView *alert;

    switch (state)

    {

        case GKPeerStateConnected:{

            NSLog(@"connected");

            alert = [[UIAlertView allocinitWithTitle:@"Bluetooth connection"message:@"connected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nilnil];

            [alert show];

        }

            break;

        case GKPeerStateDisconnected:{

            NSLog(@"disconnected");

            alert = [[UIAlertView allocinitWithTitle:@"Bluetooth connection"message:@"disconnected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nilnil];

            [alert show];

            gkSession = nil;

        }

        case GKPeerStateAvailable:

        case GKPeerStateConnecting:

        case GKPeerStateUnavailable:

            break;

    }

}

- (void)session:(GKSession *)session didFailWithError:(NSError *)error{

    NSLog(@"Session error: %@", [error description]);

}

- (void) mySendDataToPeers: (NSData *) data

{

    [gkSession sendDataToAllPeers: data withDataModeGKSendDataReliable errornil];

}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context

{

    // Read the bytes in data and perform an application-specific action.

}


Congrats, you have now successfully implemented the bluetooth programming. Use the mySendDataToPeers to send data. And receiveData to read the data received.

Xcode : EXE_BAD_ACCESS

Struggling with EXC_BAD_ACCESS ?

This happens when you are trying to access an object that has already been released.
In order to find the root of the problem you will need to turn on Zombie objects. This prevents the releasing of memory of any objects used during run time and can help you determine the source of your troubles.
To enable Zombie objects in XCode 4.2:
  1. Press ⌥⌘R (Alt – Command – R)
  2. From the tab “Info | Arguments | Options | Diagnostics” select Diagnostics and click “Enable Zombie Objects”.

From now on, released objects will turn into zombies and will appear in the debugger stack trace