Sunday, December 15, 2013

Recovering a deleted App Name in Apple Store


If you want to use a App Name in your new Apple Developer Account, which already used in your another apple account, even though the status is "Developer Removed From Sale". iTunes Connect will disallow you,  the error message will be like "the app name you entered has already been used".


There is a solution based on the following link: http://hesh.am/2012/01/recovering-a-deleted-app-name-in-itunes-connect/


As long as you change your default language when you creating your new app, iTunes Connect will allow you to create a new app even they have the same name.

For example , change the default language from Australian English to English.

Wednesday, November 27, 2013

iOS Interview Questions

From: http://www.raywenderlich.com/53962/ios-interview-questions


1. Explain method swizzling. When you would use it?

2.Take three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent. Explain what happens.

3. What happens when you invoke a method on a nil pointer?  

4. Give two separate and independent reasons why retainCount should never be used in shipping code.

5. Explain your process for tracing and fixing a memory leak.

6. Explain how an autorelease pool works at the runtime level.

7. When dealing with property declarations, what is the difference between atomic and non-atomic?

8. In C, how would you reverse a string as quickly as possible?

9. Which is faster: to iterate through an NSArray or an NSSet

10. Explain how code signing works.

11. What is posing in Objective-C?

12. List six instruments that are part of the standard.

13. What are the differences between copy and retain?

14. What is the difference between frames and bounds?

15. What happens when the following code executes? Ball *ball = [[[[Ball alloc] init] autorelease] autorelease];

16. List the five iOS app states.

17. Do you think this interview was a good representation of your skills as a developer?

Thursday, October 3, 2013

Learning Python -- Part two

1. Dictionary

    dic = {'lilei':90,'lily':100,'sam':57, ''tom" :90}
   
    for key in dic
        print dic[key]

    print dic.keys(),  return all keys
            dic.values(), return all values
            dic.items(),  return all element
            dic.clear(),  clear dic, dic become {}

   del dic['tom']  , delete 'tom' in dic, del is the keyword
   
2. file
   
    f = open(filename, mode), "r" read, "w" write
    content  = f.read(N) , read N bytes data
    content  = f.readline(), read one line
    content = f.readlines(), read all the lines

    f.write('I like apple'), write to file

    f.close()

3. range(), enumerate(), zip()
    S = 'abcdefghijk'
    for i in range(0,len(S),2):
        print S[i]
 
    for (index,char) in enumerate(S):
        print index
        print char
 
    ta =[1,2,3]
    tb = [9,8,7]
    tc = ['a','b','c']
    for (a,b,c) in zip(ta,tb,tc)
        print(a,b,c)

    ta = [1,2,3]
    tb = [9,8,7]
    zipped = zip(ta,tb)
    print(zipped)

    na,nb = zip(*zipped)
    print(na,nb)


4. lambda
    func = lambda x,y: x+ y
    print func(3,4)

    def teset(f,a,b):
        print 'test'
        print f(a,b)
   test(func,3,5)

   test((lambda x: x+3),[1,3,5,6])

5. map
    re = map((lambda x,y: x+y),[1,2,3],[6,7,9])

6. filter
    def func(a)
        if a> 100:
            return True
        else:
            return False

   print filter(func,[10,56,101,500])

7. reduce
    print reduce((lambda x,y: x+y),[1,2,5,7,9])

8.  debug
     re = iter(range(5))

     try:
         for i in range(100):
             print re.next()
     except StopIteration:
         print 'Here is end ',i
    print 'HaHaHa'


    try:
        ...
    except error1:
        ...
    except error2:
        ...
    else:
        ...
    finally:
        ...

Wednesday, October 2, 2013

Learning Python - part one

1.  print 'Hello World'
     a = 10
     print a
     print type(a)
 
     a = 1.3
     print a
     print type(a)

     basic type:
     a = 10     int     a =1.3 float       a = True  True/False    a = 'Hello'  string
     type() - return data type
 
 2.  Sequence:
      Tuple : s1 = (2, 1.3, 'love',9,12,false)  , element can not be change
     
      List: s2 = [True, 5, 'smile']  , element can change

      print s1[0]
      print s2[2]
      s2[1] = 3.0
      print s2

      print s1[:5]  -- from start to element 4
      print s1[2:]  -- from element 2 to last
      print s1[0:5:2]  -- from index 0 to element 4 , every 2 to get a element, such as element 0,2,4
      print s1[2:0:-1] -- from element 2 to element 1

      print s1[-1] -- last element
      print s1[-3] -- the last third element

3. If else
    i = 1
    if i>0:
        print 'positive i'
        i = i+1
    elif i ==0:
        print 'i is 0'
        i = i *10
    else:
       print 'negative i'
       i = i -1
    print 'new i:',i

4.  for, while, continue, break
   
     for a in [3,4.4, 'lift']:
         print a

     while i< 10:
         print i
         i = i+1

5. function
    def square_sum(a,b)
        c = a**2 + b ** 2
        return c

    print square_sum(3,4)

6. class , object
    class Bird(object)
        have_feather = True
        way_of_reproduction = 'egg'
        def move(self,dx,dy):
            position =[0,0]
            position[0] = position[0]+dx
            position[1] = position[1] +dy
            return position

 summer = Bird()
 print 'after move:', summer.move(5,8)

 class Chicken(Bird):
     way_of_move = 'walk'
     possible_in_KFC = True

summer = Chicken()
print summer.have_feather
print summer.move(5,6)


7. dir(), help()
    print dir(list)
    print help(list)

    n1 = [1,2,5,3,5]
    n1.count(5),   how many 5
    n1.index(3),   index of the first 3
    n1.append(6), add 6 to the list
    n1.sort(), sort
    n1.pop(), remove the last element and return it
    n1.remove(2), remove the first 2
    n1.insert(0,9), insert 9 to position 0
 

Monday, September 16, 2013

iOS -- Core Image


  • CIContext. All of the processing of a core image is done in a CIContext. This is somewhat similar to a Core Graphics or OpenGL context.
  • CIImage. This class hold the image data. It can be creating from a UIImage, from an image file, or from pixel data.
  • CIFilter. The filter class has a dictionary that defines the attributes of the particular filter that it represents. Examples of filters are vibrance filters, color inversion filters, cropping filters, and much more.

1.declare
@implementation ViewController
{

    CIContext *context;
    CIFilter *filter;
    CIImage *beginImage;

}

2. change

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"image" ofType:@"png"];
    
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    
    beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];
    
    context = [CIContext contextWithOptions:nil];
    
    filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues:kCIInputImageKey,beginImage,@"inputIntensity",@0.8, nil];
    
    CIImage *outputImage = [filter outputImage];
    
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

    UIImage *newImage = [UIImage imageWithCGImage:cgimg];
    

    self.imageView.image = newImage;

Sunday, September 15, 2013

iOS - URL SCHEME


To configure URL SCHEME

In Xcode project, info.




Open the app in code:

  NSString *format = @"birdland://";
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:format]];
   
    [[UIApplication sharedApplication] openURL:url];

Wednesday, August 28, 2013

iOS - KVO programming

declare object:
interface StockData : NSObject {
   NSString * stockName;
   float price;
}
@end
@implementation StockData
@end

Add Observer:
[stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];  

Set Value:
myLabel.text = [stockForKVO valueForKey:@"price"];  

Reset Value, trigger observer method:
[stockForKVO setValue:@"20.0" forKey:@"price"];  


Call observer method:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"price"])
    {
        myLabel.text = [stockForKVO valueForKey:@"price"];
    }

}

Remove observer:
- (void)dealloc
{
    [super dealloc];
    [stockForKVO removeObserver:self forKeyPath:@"price"];
    [stockForKVO release];
}

Wednesday, August 14, 2013

iOS- Notes for Predicates Programming Guide

This is my notes for reading Apple Predicates Programming Guide

Reference: http://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/Predicates/Articles/pUsing.html


Introduction to Predicates Programming Guide

A predicate is a logical operator that returns a Boolean value (true or false). There are two types of predicate;

comparison predicates, and compound predicates:
A comparison predicate compares two expressions using an operator.

A compound predicate compares the results of evaluating two or more other predicates, or negates
another predicate.


Predicate Classes
Cocoa provides three predicate classes: NSPredicate, and two subclasses of NSPredicate,

NSComparisonPredicate and NSCompoundPredicate.

Predicate expressions in Cocoa are represented by instances of NSExpression.


Creating Predicates
Creating a Predicate Using a Format String

NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"(lastName like[cd] %@) AND (birthday > %@)",
lastNameSearchString, birthdaySearchDate];
(In the example, like[cd] means“case- and diacritic-insensitive like.”)

String Constants, Variables, and Wildcards
String constants must be quoted within the expression—single and double quotes are both acceptable, but must be paired appropriately

NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"lastName like[c] \"S*\""];

You must take automatic quotation into account when using wildcards—you must add wildcards to a variable prior to substitution, as shown in the following example.

NSString *prefix = @"prefix";
NSString *suffix = @"suffix";
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"SELF like[c] %@",
[[prefix stringByAppendingString:@"*"] stringByAppendingString:suffix]];
BOOL ok = [predicate evaluateWithObject:@"prefixxxxxxsuffix"];


Boolean Values
NSPredicate *newPredicate =
[NSPredicate predicateWithFormat:@"anAttribute == %@", [NSNumber
numberWithBool:aBool]];
NSPredicate *testForTrue =
[NSPredicate predicateWithFormat:@"anAttribute == YES"];

Dynamic Property Names
Because string variables are surrounded by quotation marks when they are substituted into a format string using %@, you cannot use %@ to specify a dynamic property name—as illustrated in the following example.

NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ like %@",
attributeName, attributeValue];

The predicate format string in this case evaluates to "firstName" like "Adam".

If you want to specify a dynamic property name, you use %K in the format string, as shown in the following fragment.

predicate = [NSPredicate predicateWithFormat:@"%K like %@",
attributeName, attributeValue];

The predicate format string in this case evaluates to firstName like "Adam" (note that there are no
quotation marks around firstName).

Creating Predicates Directly in Code

NSComparisonPredicate and
NSCompoundPredicate provide convenience methods that allow you to easily create compound and
comparison predicates respectively. NSComparisonPredicate provides a number of operators ranging from simple equality tests to custom functions.

The following example shows how to create a predicate to represent (revenue >= 1000000) and (revenue < 100000000). Note that the same left-hand side expression is used for both comparison predicates.

NSExpression *lhs = [NSExpression expressionForKeyPath:@"revenue"];

NSExpression *greaterThanRhs = [NSExpression expressionForConstantValue:[NSNumber
numberWithInt:1000000]];
NSPredicate *greaterThanPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:greaterThanRhs
modifier:NSDirectPredicateModifier
type:NSGreaterThanOrEqualToPredicateOperatorType
options:0];

NSExpression *lessThanRhs = [NSExpression expressionForConstantValue:[NSNumber
numberWithInt:100000000]];
NSPredicate *lessThanPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:lessThanRhs
modifier:NSDirectPredicateModifier
type:NSLessThanPredicateOperatorType
options:0];

NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:
@[greaterThanPredicate, lessThanPredicate]];


Creating Predicates Using Predicate Templates

NSPredicate *predicateTemplate = [NSPredicate
predicateWithFormat:@"lastName like[c] $LAST_NAME"];

To create a valid predicate to evaluate against an object, you use the NSPredicate method
predicateWithSubstitutionVariables: to pass in a dictionary that contains the variables to be
substituted. (Note that the dictionary must contain key-value pairs for all the variables specified in the predicate.)

NSPredicate *predicate = [predicateTemplate predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObject:@"Turner" forKey:@"LAST_NAME"]];

The new predicate returned by this example is lastName LIKE[c] "Turner

Because the substitution dictionary must contain key-value pairs for all the variables specified in the predicate, if you want to match a null value, you must provide a null value in the dictionary, as illustrated in the following example.

NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"date = $DATE"];
predicate = [predicate predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"DATE"]];

The predicate formed by this example is date ==

Format String Summary
@"attributeName == %@"
This predicate checks whether the value of the key attributeName isthe same asthe value of the object
%@ that is supplied at runtime as an argument to predicateWithFormat:. Note that %@ can be a
placeholder for any object whose description is valid in the predicate, such as an instance of NSDate,
NSNumber, NSDecimalNumber, or NSString.

@"%K == %@"
This predicate checks whether the value of the key %K is the same as the value of the object %@. The
variables are supplied at runtime as arguments to predicateWithFormat:.

@"%K == '%@'"
This predicate checks whether the value of the key %K is equal to the string literal “%@“ (note the single quotes around %@). The key name %K is supplied at runtime as an argument to predicateWithFormat:.

@"name IN $NAME_LIST"
This is a template for a predicate that checks whether the value of the key name is in the variable
$NAME_LIST (no quotes) that is supplied at runtime using predicateWithSubstitutionVariables:

@"'name' IN $NAME_LIST"
This is a template for a predicate that checks whether the constant value 'name' (note the quotes around
the string) is in the variable $NAME_LIST that is supplied at runtime using
predicateWithSubstitutionVariables:.

@"$name IN $NAME_LIST"
This is a template for a predicate that expects values to be substituted (using
predicateWithSubstitutionVariables:) for both $NAME and $NAME_LIST.

Using Predicates
Evaluating Predicates
To evaluate a predicate, you use the NSPredicate method evaluateWithObject: and pass in the object
against which the predicate will be evaluated. The method returns a Boolean value—in the following example,
the result is YES.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"Stig",
@"Shaffiq", @"Chris"]];
BOOL result = [predicate evaluateWithObject:@"Shaffiq"];

Using Predicates with Arrays
NSArray and NSMutableArray provide methods to filter array contents. NSArray provides
filteredArrayUsingPredicate: which returns a new array containing objects in the receiver that match
the specified predicate. NSMutableArray provides filterUsingPredicate: which evaluates the receiver’s
content against the specified predicate and leaves only objects that match.

NSMutableArray *array =
[NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", nil];
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c]
'a'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];
// beginWithB contains { @"Adam" }.
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c]
'e'"];
[array filterUsingPredicate:sPredicate];
// array now contains {@"Ben", @"Melissa" }

Using Predicates with Key-Paths
NSString *departmentName = ... ;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"department.name like %@", departmentName];

If you use a to-many relationship, the construction of a predicate is slightly different. If you want to fetch Departments in which at least one of the employees has the first name "Matthew," for instance, you use an ANY operator as shown in the following example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"ANY employees.firstName like 'Matthew'"];

If you want to find Departments in which at least one of the employees is paid more than a certain amount, you use an ANY operator as shown in the following example:

float salary = ... ;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY employees.salary
> %f", salary];

Null Values
A comparison predicate does not match any value with null except null (nil) or the NSNull null value (that is, ($value == nil) returns YES if $value is nil)

Testing for Null
If you want to match null values, you must include a specific test in addition to other comparisons, as illustrated in the following fragment.

predicate = [NSPredicate predicateWithFormat:@"(firstName == %@) || (firstName =
nil)", firstName];
filteredArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"filteredArray: %@", filteredArray);
// Output:
// filteredArray: ( { lastName = Turner; }, { birthday = 1972-03-23 20:45:32 -0800;
firstName = Ben; lastName = Ballard; }

By implication, a test for null that matches a null value returns true. In the following code fragment, ok is set to YES for both predicate evaluations.

predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];
BOOL ok = [predicate evaluateWithObject:[NSDictionary dictionary]];
ok = [predicate evaluateWithObject:
[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"firstName"]];

Using Predicates with Core Data
Fetch Requests
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSNumber *salaryLimit = <#A number representing the limit#>;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salary > %@",
salaryLimit];
[request setPredicate:predicate];
NSError *error;

NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];

Regular Expressions
NSArray *array = @[@"TATACCATGGGCCATCATCATCATCATCATCATCATCATCATCACAG",
@"CGGGATCCCTATCAAGGCACCTCTTCG", @"CATGCCATGGATACCAACGAGTCCGAAC",
@"CAT", @"CATCATCATGTCT", @"DOG"];
// find strings that contain a repetition of at least 3 'CAT' sequences,
// but not followed by a further 'CA'
NSPredicate *catPredicate =
[NSPredicate predicateWithFormat:@"SELF MATCHES '.*(CAT){3,}(?!CA).*'"];
NSArray *filteredArray = [array filteredArrayUsingPredicate:catPredicate];
// filteredArray contains just 'CATCATCATGTCT'

According to the ICU specification, regular expression meta characters are not valid inside a pattern set. For example, the regular expression \d{9}[\dxX] does not match valid ISBN numbers (any ten digit number, or a string with nine digits and the letter 'X')since the pattern set ([\dxX]) contains a meta character (\d). Instead you could write an OR expression, as shown in the following code sample:

NSArray *isbnTestArray = @[@"123456789X", @"987654321x", @"1234567890", @"12345X",
@"1234567890X"];
NSPredicate *isbnPredicate =
[NSPredicate predicateWithFormat:@"SELF MATCHES '\\\\d{10}|\\\\d{9}[Xx]'"];
NSArray *isbnArray = [isbnTestArray filteredArrayUsingPredicate:isbnPredicate];
// isbnArray contains (123456789X, 987654321x, 1234567890)

Performance
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"( title matches .*mar[1-10] ) OR ( type = 1 )"];
you should write
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"( type = 1 ) OR ( title matches .*mar[1-10] )"];

In the second example, the regular expression is evaluated only if the first clause is false.

Predicate Format String Syntax
Parser Basics

Variables are denoted with a $ (for example $VARIABLE_NAME). ? is not a valid parser token.

%@ is a var arg substitution for an object value—often a string, number, or date.

%K is a var arg substitution for a key path.


When string variables are substituted into a format string using %@ , they are surrounded by quotation marks. If you want to specify a dynamic property name, use %K in the format string, as shown in the following example.

NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",
attributeName, attributeValue];

The predicate format string in this case evaluates to firstName like "Adam“

Basic Comparisons
Boolean Value Predicates

TRUEPREDICATE
A predicate that always evaluates to TRUE.
FALSEPREDICATE
A predicate that always evaluates to FALSE.

String Comparisons
BEGINSWITH
The left-hand expression begins with the right-hand expression.

CONTAINS
The left-hand expression contains the right-hand expression.

ENDSWITH
The left-hand expression ends with the right-hand expression.

LIKE
The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters,
where ? matches 1 character and * matches 0 or more characters

MATCHES
The left hand expression equals the right hand expression using a regex-style comparison according to
ICU v3

Aggregate Operations
ANY, SOME

Specifies any of the elements in the following expression. For example ANY children.age < 18.

ALL

Specifies all of the elements in the following expression. For example ALL children.age < 18.

NONE
Specifies none of the elements in the following expression. For example, NONE children.age < 18.

This is logically equivalent to NOT (ANY ...).

IN
Equivalent to an SQL IN operation, the left-hand side must appear in the collection specified by the
right-hand side.
For example, name IN { 'Ben', 'Melissa', 'Nick' }. The collection may be an array, a set, or a
dictionary—in the case of a dictionary, its values are used.
In Objective-C, you could create a IN predicate as shown in the following example:

NSPredicate *inPredicate =
[NSPredicate predicateWithFormat: @"attribute IN %@", aCollection];

where aCollection may be an instance of NSArray, NSSet, NSDictionary, or of any of the
corresponding mutable classes.

Reserved Words

The following words are reserved:
AND, OR, IN, NOT, ALL, ANY, SOME, NONE, LIKE, CASEINSENSITIVE, CI, MATCHES, CONTAINS, BEGINSWITH,
ENDSWITH, BETWEEN, NULL, NIL, SELF, TRUE, YES, FALSE, NO, FIRST, LAST, SIZE, ANYKEY, SUBQUERY, CAST,

TRUEPREDICATE, FALSEPREDICATE



Tuesday, August 6, 2013

iOS- Create push notification private key by shell script

To use the push notification, you need to create a  p12 private key file to your server side. I would like to create a shell script for it. Then you can reuse it for some time.

First of all, you need to three source file :1.certSigningRequest, 2.p12(private key for the certificate), 3.cer(provision profile)

for example, using .Net to send push notification, you need to create a p12 file, you need to type 3 lines of command, which is

then you need to type the followinig command as below:

openssl x509 -in 3.cer -inform der -out PushCert.pem

openssl pkcs12 -nocerts -out PushKey.pem -in 2.p12


openssl pkcs12 -export -in PushCert.pem -inkey PushKey.pem -certfile 1.certSigningRequest -name "apn_production_identity" -out apn_production_identity.p12

Step one:
Open your terminal, type vim my_script

Step two:
input the following words:

#!/bin/bash
# Create push p12 file

openssl x509 -in 3.cer -inform der -out PushCert.pem

openssl pkcs12 -nocerts -out PushKey.pem -in 2.p12

openssl pkcs12 -export -in PushCert.pem -inkey PushKey.pem -certfile 1.certSigningRequest -name "apn_production_identity" -out apn_production_identity.p12



Step three:
save the file and put the source files and my_script in the same folder

Step four:
Open terminal, cd to the folder, run
bash my_script

After enter the password to verify, then you will get the p12 file, which is apn_production_identity.p12







Monday, July 29, 2013

iOS- NSString to NSDate and NSDate to NSString

Here is the code snippet to show NSString convert to NSDate and NSDate convert to NSString


Input Date string and date formate
for example [aa dateFromString:@"your date" and @"dd/MM/yyyy hh:mm"];

+(NSDate*)dateFromString: (NSString*)dateString and: (NSString*)format
{
    
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];

    // Server sends Sydney time
    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Australia/Sydney"];
    [dateformatter setTimeZone:zone];

    // set locale, we use us date time style,
   // if you want to force to display 12 hour style or 24 hour style, you can set  en_US_POSIX force to     use 12 hour style
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    // date string to NSdate
    [dateformatter setLocale:locale];
    [dateformatter setDateFormat:format];
    NSDate *date =[dateformatter dateFromString:dateString];

    return  date;
}

+(NSString*)stringFromDate: (NSDate*)date and: (NSString*)format
{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:format];
     NSTimeZone *zone = [NSTimeZone localTimeZone];
    [dateFormat setTimeZone:zone];
    
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    [dateFormat setLocale:locale];
    
     return [dateFormat stringFromDate:date];

}

Concurrency in iOS

First of all, let's understand some basic concept:
Main queue
This queue performs all its tasks on the main thread, which is where Cocoa and Cocoa Touch require programmers to call all UI-related methods. Use the dispatch_get_main_queue function to retrieve the handle on the main queue.

Concurrent queues
These are queues that you can retrieve from GCD in order to execute asynchronous or synchronous tasks. Use the dispatch_get_global_queue function to retrieve the handle to a concurrent queue.

Serial queues
These are queues that, no matter whether you submit synchronous or asynchronous tasks to them, will always execute their tasks in the first-in-first-out(FIFO) fashion. Use the dispatch_queue_create to create a serial queue. Once you are done with the queue, you must release it using dispath_release function.

Only one main queue, but you can create as many serial dispatch queues as you want. Tasks can be handed to dispatch queues in two forms: block objects or C functions.

Main subjects to handle concurrency, Grand Central Dispatch, Threads,Timers,Operation and Operation Queues.


Grand Central Dispatch
Performing UI-Related Tasks with GCD

use dispatch_get_main_queue, UI-related tasks have to be performed on the main thread.
Code Snippet,

       dispatch_queue_t mainQueue = dispatch_get_main_queue(); 

       dispatch_async(mainQueue, ^(void) {
       [[[UIAlertView alloc] initWithTitle:@"GCD" message:@"GCD is amazing!"
           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 
       });

Executing Non-UI Related Tasks Synchronously with GCD
use the dispatch_sync function



              void (^printFrom1To1000)(void) = ^{
              NSUInteger counter = 0; for (counter = 1;
              counter <= 1000; counter++){
              NSLog(@"Counter = %lu - Thread = %@", (unsigned long)counter,
              [NSThread currentThread]);
              } }; 


dispatch_queue_t concurrentQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

           dispatch_sync(concurrentQueue, printFrom1To1000); 
           dispatch_sync(concurrentQueue, printFrom1To1000); 

The priority of the concurrent queue that GCD has to retrieve for the programmer.

DISPATCH_QUEUE_PRIORITY_LOW
Fewer timeslices will be applied to your task than normal tasks.

DISPATCH_QUEUE_PRIORITY_DEFAULT
The default system priority for the code execution will be applied to your task.

DISPATCH_QUEUE_PRIORITY_HIGH
More timeslices will be applied to your task than normal tasks.

Executing Non-UI Related Tasks Asychronously with GCD
Use dispatch_async


          dispatch_queue_t concurrentQueue =              dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_async(concurrentQueue, ^{
              __block UIImage *image = nil;
             dispatch_sync(concurrentQueue, ^{ /* Download the image here */
             });
        dispatch_sync(dispatch_get_main_queue(), ^{

      /* Show the image to the user here on the main queue*/

        }); }); 


Performing Tasks After a Delay with GCD
use the dispatch_after and dispatch_after_f functions


             double delayInSeconds = 2.0;
             dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

             dispatch_queue_t concurrentQueue =          dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 


             dispatch_after(delayInNanoSeconds, concurrentQueue, ^(void){ /* Perform your operations here */
}); 

Performing a Task Only Once with GCD
use the dispatch_once function


Friday, July 19, 2013

Core Data Programming Guide Notes -- Part 6

This is the notes of Core Data Programing Guide from reading Apple's document

reference: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP30001200-SW1


Core Data FAQ
Where does a Managed Object Context Come From? 

Where a managed object context comes from is entirely application-dependent. In a Cocoa document-based application using NSPersistentDocument, the persistent document typically creates the context, and gives you access to it through the managedObjectContext method. 



How do I initialize a store with default data? 

There are two issues here: creating the data, and ensuring the data is imported only once.

There are several ways to create the data.

  • You can create a separate persistent store that contains the default data and include the store as an application resource. When you want to use it, you must either copy the whole store to a suitable location, or copy the objects from the defaults store to an existing store.
  • For small datasets, you can create the managed objects directly in code.
  • You can create a property list—or some other file-based representation—of the data, and store it as an application resource. When you want to use it, you must open the file and parse the representation to create managed objects. 

How do I use my existing SQLite database with Core Data?

You don’t. Although Core Data supports SQLite as one of its persistent store types, the database format is private. You cannot create a SQLite database using native SQLite API and use it directly with Core Data (nor should you manipulate an existing Core Data SQLite store using native SQLite API). If you have an existing SQLite database, you need to import it into a Core Data store


 How do I fetch objects in the same order I created them?
 Objects in a persistent store are unordered. Typically you should impose order at the controller or view layer, based on an attribute such as creation date. If there is order inherent in your data, you need to explicitly model that.
 






Core Data Programming Guide Notes -- Part 5

This is the notes of Core Data Programing Guide from reading Apple's document




Concurrency with Core Data

You can use threads, serial operation queues, or dispatch queues for concurrency. For the sake of conciseness, this article uses “thread” throughout to refer to any of these.

Use Thread Confinement to Support Concurrency 
The pattern recommended for concurrent programming with Core Data is thread confinement : each thread must have its own entirely private managed object context. 

There are two possible ways to adopt the pattern:
  1. Create a separate managed object context for each thread and share a single persistent store coordinator.
    This is the typically-recommended approach. 
      2.Create a separate managed object context and persistent store coordinator for each thread.
         This approach provides for greater concurrency at the expense of greater complexity (particularly            if you need to communicate changes between different contexts) and increased memory usage. 

You must create the managed context on the thread on which it will be used. If you use NSOperation, note that its init method is invoked on the same thread as the caller. You must not, therefore, create a managed object context for the queue in the queue’s init method, otherwise it is associated with the caller’s thread. Instead, you should create the context in main (for a serial queue) or start (for a concurrent queue).

Using thread confinement, you should not pass managed objects or managed object contexts between threads. To “pass” a managed object from one context another across thread boundaries, you either:

  •  Pass its object ID (objectID) and use objectWithID: or existingObjectWithID:error: on the receiving managed object context. 

          The corresponding managed objects must have been saved—you cannot pass the ID of a newly-             inserted managed object to another context. 

  •  Execute a fetch on the receiving context.


Track Changes in Other Threads Using Notifications


Typically, on thread A you register for the managed object context save notification, NSManagedObjectContextDidSaveNotification. When you receive the notification, its user info dictionary contains arrays with the managed objects that were inserted, deleted, and updated on thread B. Because the managed objects are associated with a different thread, however, you should not access them directly. Instead, you pass the notification as an argument to mergeChangesFromContextDidSaveNotification: (which you send to the context on thread A). Using this method, the context is able to safely merge the changes. 


Fetch in the Background for UI Responsiveness 

Following the thread confinement pattern, you use two managed object contexts associated with a single persistent store coordinator. You fetch in one managed object context on a background thread, and pass the object IDs of the fetched objects to another thread. In the second thread (typically the application's main thread, so that you can then display the results), you use the second context to fault in objects with those object IDs (you use objectWithID: to instantiate the object). (This technique is only useful if you are using an SQLite store, since data from binary and XML stores is read into memory immediately on open.)


Saving in a Background Thread is Error-prone

If you perform a save operation in a background thread, therefore, it may be killed before it is able to complete. If you need to save on a background thread, you must write additional code such that the main thread prevents the application from quitting until all the save operation is complete. 

If You Don’t Use Thread Containment
If you choose not to use the thread containment pattern—that is, if you try to pass managed objects or contexts between threads, and so on—you must be extremely careful about locking, and as a consequence you are likely to negate any benefit you may otherwise derive from multi-threading. You also need to consider that:

  • Any time you manipulate or access managed objects, you use the associated managed object context. Core Data does not present a situation where reads are “safe” but changes are “dangerous”—every operation is “dangerous” because every operation has cache coherency effects and can trigger faulting. 
  • Managed objects themselves are not thread safe. If you want to work with a managed object across different threads, you must lock its context (seeNSLocking). 

Typically you lock the context or coordinator using tryLock or lock. If you do this, the framework will ensure that what it does behind the scenes is also thread-safe. For example, if you create one context per thread, but all pointing to the same persistent store coordinator, Core Data takes care of accessing the coordinator in a thread-safe way (the lock and unlock methods of NSManagedObjectContext handle recursion).
If you lock (or successfully tryLock) a context, you must keep a strong reference to that context until you invoke unlock. If you don’t, in a multi-threaded environment, you may cause a deadlock. 



Core Data Performance

Fetching Managed Objects 
 Fetch Predicates
 How you use predicates can significantly affect the performance of your application.
 Especially if the predicate involves text matching (contains, endsWith, like, and matches) since correct Unicode searching is slow. If the predicate combines textual and non-textual comparisons, then it is likely to be more efficient to specify the non-textual predicates first, for example (salary > 5000000) AND (lastName LIKE 'Quincey') is better than (lastName LIKE 'Quincey') AND (salary > 5000000).
 


Fetch Limits

You can set a limit to the number of objects a fetch will return using the method setFetchLimit: as shown in the following example.

NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setFetchLimit:100];

Faulting Behavior
You can safely invoke the following methods on a fault without causing it to fire: isEqual:, hash, superclass, class, self, zone, isProxy, isKindOfClass:, isMemberOfClass:, conformsToProtocol:, respondsToSelector:, description, managedObjectContext, entity, objectID, isInserted, isUpdated, isDeleted, and isFault. 

Batch Faulting and Pre-fetching with the SQLite Store

When you execute a fetch, Core Data fetches just instances of the entity you specify. 


Analyzing Performance 

Analyzing Fetch Behavior with SQLite
With OS X version 10.4.3 and later, you can use the user default com.apple.CoreData.SQLDebug to log to stderr the actual SQL sent to SQLite. (Note that user default names are case sensitive.) For example, you can pass the following as an argument to the application:

-com.apple.CoreData.SQLDebug 1

 






Troubleshooting Core Data

Object Life-Cycle Problems 

Merge errors 

Problem: You see the error message, "Could not merge changes".
Cause: Two different managed object contexts tried to change the same data. This is also known as an optimistic
locking failure.
Remedy: Either set a merge policy on the context, or manually (programmatically) resolve the failure. You can retrieve the currently committed values for an object using committedValuesForKeys:, and you can re-fault the object (so that when it is next accessed its data values are retrieved from its persistent store) using refreshObject:mergeChanges:.


Assigning a managed object to a different store 
Problem: You see an exception that looks similar to this example. 
  [_assignObject:toPersistentStore:]:
Can’t reassign an object to a different store once it has been saved.
Cause: The object you are trying to assign to a store has already been assigned and saved to a different store. Remedy: To move an object from one store to another, you must create a new instance, copy the information
from the old object, save it to the appropriate store, and then delete the old instance.
Fault cannot be fulfilled
Problem: You see the error message, "Core Data could not fulfill a fault". Cause: The corresponding object's underlying data has been deleted from the persistent store. Remedy: You should discard this object. This problem can occur in at least two situations:
First:
Start with a strong reference to a managed object.
Delete the managed object via the managed object context.
Save changes on the object context.
At this point, the deleted object has been turned into a fault. It isn’t destroyed because doing so would violate the rules of memory management.
Try to retrieve an attribute or relationship from the previously retained reference.
Core Data will try to fault the faulted managed object but will fail to do so because the object has been deleted from the store. That is, there is no longer an object with the same global ID in the store.
Second:
Delete an object from a managed object context. Fail to break all relationships from other objects to that object. Save changes.
Managed object invalidated
Problem: You see an exception that looks similar to this example:
[_assignObject:toPersistentStore:]: The NSManagedObject with ID:#### has been invalidated.
Cause: Either you have removed the store for the fault you are attempting to fire, or the managed object's context has been sent a reset message.
Remedy: You should discard this object. If you add the store again, you can try to fetch the object again.
Class is not key-value coding compliant
Problem: You see an exception that looks similar to the following example.
 [ valueForUndefinedKey:]:
this class is not key value coding-compliant for the key randomKey.
Cause: Either you used an incorrect key, or you initialized your managed object with init instead of initWithEntity:inManagedObjectContext:.
Remedy: Use a valid key (check the spelling and case carefully—also review the rules for key-value coding compliance in Key-Value Coding Programming Guide ), or ensure that you use the designated initializer for NSManagedObject (see initWithEntity:insertIntoManagedObjectContext:).
Efficiently Importing Data

Cocoa Fundamentals