Multithreading with Core Data

…….
…….
…….
@synchronized(self)
{

  1.     Create new NSManagedObjectContext object
  2.     Set persistent store coordinator of main thread’s NSManagedObjectContext to new NSManagedObjectContext object
  3.     Fetch NSManagedObject from DB using new NSManagedObjectContext object
  4.     Do modifications on NSManagedObject object
  5.     save new NSManagedObjectContext object
  6.     Merge new NSManagedObjectContext object changes with Main thread’s NSManagedObjectContext object

}
…….
…….
…….

Make use of onFinishParsing method from the following link
http://stackoverflow.com/questions/2009399/cryptic-error-from-core-data-nsinvalidargumentexception-reason-referencedata64

Blogged with the Flock Browser

Tags: , , , , , ,

How to check the character is backspace (delete)


- (BOOL) textField:(UITextField *) textField
shouldChangeCharactersInRange:(NSRange) range
            replacementString:(NSString *) string
{
     if ([string length])
     {
          if ([string characterAtIndex:0] == 10)
          {
               NSLog(@"keyboard backspace pressed");
          }
      }
      return YES;
}

Blogged with the Flock Browser

Basic topics a mac developer should know

  • MVC Pattern
  • Memory Management
  • Archiving Data
  • User Defaults
  • Localization
  • Categories
  • Formal and informal Protocols
  • Key Value coding
  • Key Value observing
  • Custom Drawing
  • Notifications
  • Delegation
  • Blocks
  • Threads
  • Core Animation
  • Debugging
  • Instruments
  • Predicates
  • Custom Drawing
  • Bindings
Blogged with the Flock Browser

NSURLConnection Usage

– (void)sendRequestToServer:(NSURL *)url httpBody:(NSString *)methodCallString
{

    NSMutableURLRequest *xmlrpcRequest =[NSMutableURLRequest requestWithURL:url
                                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                            timeoutInterval:60.0];
    [xmlrpcRequest setHTTPMethod:@”POST”];
    [xmlrpcRequest setValue:@”text/xml” forHTTPHeaderField:@”Content-Type”];
   
    // set the HTTPBody with the method call string as bytes
    const char *utfString = [methodCallString UTF8String];
   
    NSString *utfStringLenString = [NSString stringWithFormat:@”%u”, strlen(utfString)];
   
    [xmlrpcRequest setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]];
   
    // Set the Content-Length header
    [xmlrpcRequest setValue:utfStringLenString forHTTPHeaderField:@”Content-Length”];
   
    // create the connection with the request and start loading the data
    if (!isConnectionBusy) {
        isConnectionBusy = YES;
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:xmlrpcRequest delegate:self];
       
        if (theConnection) {
            responseData = [[NSMutableData data] retain];
        } else {
            NSLog(@”Connection Failed!”);
        }
    }
}

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (responseData != NULL)
        [responseData setLength:0];
}

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    //    NSString *responseString = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease];
}

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    isConnectionBusy = NO;

    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [responseData release];
       
    // inform the user
    NSLog(@”Connection failed! Error – %@ %@”,
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

– (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    isConnectionBusy = NO;
   
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

    [connection release];
    connection = nil;
    [responseData release];

    [self parseData:responseString];
    [responseString release];
    // release the connection, and the data object
}

-(NSURLRequest *)connection:(NSURLConnection*) connection
            willSendRequest:(NSURLRequest*)    request
           redirectResponse:(NSURLResponse*) redirectResponse
{
    //NSLog(@”connection willSendRequest redirectResponse”);

    NSURLRequest *newRequest = request;
    if (redirectResponse)
    {
        newRequest=nil;
    }
    else
    {
    }
    return newRequest;
}

– (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

Blogged with the Flock Browser

Customize back bar button item title

When developing navigation controller-based apps, it’s pretty common to want to customize the title of the back button that is displayed on the navigation bar. Usually the button title is set to the parent view controller’s title, but you can customize that.

All you need to do is add some code to the viewDidLoad method in the parent view controller:

– (void) viewDidLoad
{
    [super viewDidLoad];

    self.title = @”Title goes here”;
 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@”Back”
                            style:UIBarButtonItemStyleBordered
                            target:nil
                            action:nil];

    self.navigationItem.backBarButtonItem = backButton;   // Affect child view controller’s back button.
    [backButton release];
 }

Blogged with the Flock Browser

UIScrollView: paging horizontally, scrolling vertically?

Cited from: http://stackoverflow.com/questions/728014/prevent-diagonal-scrolling-in-uiscrollview

// RemorsefulScrollView.h

@interface RemorsefulScrollView : UIScrollView {
  CGPoint _originalPoint;
  BOOL _isHorizontalScroll, _isMultitouch;
  UIView *_currentChild;
}
@end

// RemorsefulScrollView.m

// the numbers from an example in Apple docs, may need to tune them
#define kThresholdX 12.0f
#define kThresholdY 4.0f

@implementation RemorsefulScrollView

– (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    self.delaysContentTouches = NO;
  }
  return self;
}

– (id)initWithCoder:(NSCoder *)coder {
  if (self = [super initWithCoder:coder]) {
    self.delaysContentTouches = NO;
  }
  return self;
}

– (UIView *)honestHitTest:(CGPoint)point withEvent:(UIEvent *)event {
  UIView *result = nil;
  for (UIView *child in self.subviews)
    if ([child pointInside:point withEvent:event])
      if ((result = [child hitTest:point withEvent:event]) != nil)
        break;
   
    if ([result isKindOfClass:[UITableView class]])
    {
        return result;
    }
   
    return nil;
}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event]; // always forward touchesBegan — there’s no way to forward it later
    if (_isHorizontalScroll) {
        return; // UIScrollView is in charge now
    }
    if ([touches count] == [[event touchesForView:self] count]) { // initial touch
        _originalPoint = [[touches anyObject] locationInView:self];
    _currentChild = [self honestHitTest:_originalPoint withEvent:event];
    _isMultitouch = NO;
    }
  _isMultitouch = _isMultitouch || ([[event touchesForView:self] count] > 1);
  [_currentChild touchesBegan:touches withEvent:event];
}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if (!_isHorizontalScroll && !_isMultitouch) {
    CGPoint point = [[touches anyObject] locationInView:self];
    if (fabsf(_originalPoint.x – point.x) > kThresholdX && fabsf(_originalPoint.y – point.y) < kThresholdY) {
      _isHorizontalScroll = YES;
      [_currentChild touchesCancelled:[event touchesForView:self] withEvent:event];
    }
  }
  if (_isHorizontalScroll) {
    [super touchesMoved:touches withEvent:event]; // UIScrollView only kicks in on horizontal scroll
    }
  else {
    [_currentChild touchesMoved:touches withEvent:event];
    }
}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  if (_isHorizontalScroll) {
    [super touchesEnded:touches withEvent:event];
        _isHorizontalScroll = NO;
    }
    else {
    [super touchesEnded:touches withEvent:event];
    [_currentChild touchesEnded:touches withEvent:event];
    }
}

– (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  [super touchesCancelled:touches withEvent:event];
  if (!_isHorizontalScroll)
    {
    [_currentChild touchesCancelled:touches withEvent:event];
        _isHorizontalScroll = NO;
    }
}

@end

Blogged with the Flock Browser

Avoiding WiFi disconnections


Avoiding WiFi disconnections

Any iPhone application that requires a WiFi connection must set UIRequiresPersistentWiFi to <true/>, otherwise the iPhone will abruptly disconnect the WiFi connection after 30 minutes of use. No warning, no error: 30 minutes and you’ll lose WiFi without this flag.

Blogged with the Flock Browser