2010年5月31日星期一

NSOperation NSOperationQueue

The NSOperation and NSOperationQueue classes alleviate much of the pain of multi-threading, allowing you to simply define your tasks, set any dependencies that exist, and fire them off. Each task, or operation, is represented by an instance of an NSOperation class; the NSOperationQueue class takes care of starting the operations, ensuring that they are run in the appropriate order, and accounting for any priorities that have been set.

NSOperation 和 NSOperationQueue类简化了多线程操作。创建一个NSOperationQueue对象,然后创建NSOperation添加到NSOperationQueue中。NSOperationQueue会为每一个NSOperation创建一个线程,并按照它们被添加的顺序执行(或者指定的顺序)。NSOperationQueue会自己处理autorelease pools 和其他的garbage,而不需要像在自己手动创建多线程时去费心管理这些事情。

NSOperationQueue *queue = [NSOperationQueue new];

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(methodToCall)
object:objectToPassToMethod];

[queue addOperation:operation];
[operation release];


methodToCall会在新线程中被执行。


Share:

2010年5月12日星期三

modal view 上显示 navigation bar

向下实例显示modeview时不会显示上方的navigation bar

ModalViewController *modal = [[ModalViewController alloc] init];
[self presentModalViewController:model animated:YES];
[modal release];

需要这么实用才可以


ModalViewController *modal = [[ModalViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:modal];
[self presentModalViewController:nav animated:YES];
[modal release];

Share:

2010年5月7日星期五

iphone core data 基础操作

1. 插入

AppDelegate *app = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [app managedObjectContext];

NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"entityname" inManagedObjectContext:context];

[newManagedObject setValue:value forKey:@"propertyname"];

NSError *error; if (![context save:&error]) {

// Handle the error...

}

//查询
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Hero" inManagedObjectContext:managedObjectContext];

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]

initWithKey:@"name" ascending:YES];

NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]

initWithKey:@"secretIdentity" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc]

initWithObjects:sortDescriptor1, sortDescriptor2, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSArray *objecs = [context executeFetchRequest: fetchRequest error:&error];

//删除

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;

[context deleteObject:[objecs objectIndex:index];

// Save the context.

NSError *error; if (![context save:&error]) {

// Update to handle the error appropriately.

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

exit(-1); // Fail

}

//属性

NSManagedObject *managedObject;

NSString *keypath;

NSString *labelString;

NSString *currentValue = [self.managedObject valueForKeyPath:self.keypath];

NSEntityDescription *ed = [self.managedObject entity];

NSDictionary *properties = [ed propertiesByName];

NSAttributeDescription *ad = [properties objectForKey:self.keypath];

NSString *defaultValue = nil;

if (ad != nil)

defaultValue = [ad defaultValue];

//core data relation  查询 或 修改

NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext:thePerson.managedObjectContext];

NSMutableSet *children = [person mutableSetValueForKey:@"children"]; //查询,可修改

[children addObject:child];

[children removeObject:childToBeRemoved];

[[children managedObjectContext] deleteObject:childToBeRemoved]; //真正的删除




NSSet *children = [person valueForKey:@"children"]; //查询,不可修改

for (NSManagedObject *oneChild in children) {

// do something

}

补充:

1 是否将图片存储到Core Data中,以及NSData如何存储的一些规则

First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.

Second, the rule for storing binary data is:

  • < 100kb store in the same table as the relevant data

  • < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily

  • > 1mb store on disk and reference it inside of Core Data

(1。图片尽量保存为文件 2。<100k 和相关数据保存在一张表中  <1M保存在单独的一张表中 >1M保存到磁盘文件)
Share:

Objective-C runtime

#import <objc/runtime.h>

#import <objc/message.h>

This is the first time we’ve worked with the Objective-C runtime directly. Although for most programming jobs there’s no need to dive down into the runtime, having access to the same functions that are used to implement Objective-C gives us an incredible amount of power. Let’s quickly run through what we’re doing here, but don’t feel like you have to grok this one the first time through.
First, we declare an int, which will hold the number of properties that managedObject has. Then we declare a pointer to an objc_property_t, which is a datatype that represents Objective-C 2.0 properties, and use a runtime function called class_copyPropertyList() to retrieve the list of pointers to the managedObject properties. This function also populates outCount with the number of properties.

unsigned int outCount; objc_property_t *propList =

class_copyPropertyList([self.managedObject class], &outCount); Next, we use a for loop to iterate over the properties:

for (int i=0; i < outCount; i++)

{

We grab a reference to the structure that points to one property in the list, and then get the property’s name as an NSString instance. We also get the property’s attributes, which are contained in a string. The format for the attribute string is documented in Apple’s Objective-C runtime documentation, but for our purposes, all we need to know

is that it contains (among other things) the class of the property.

We check to see if the attribute string contains @"NSSet":

if ([attrs rangeOfString:@"NSSet"].location != NSNotFound) {

If it does, we then retrieve the set and create an instance of NSMutableArray to keep track of the objects that need to be deleted. It is not safe to delete objects from a collection while we are iterating over it, so we’ll stick them in an array. Then, when we’re finished iterating, we’ll iterate through the array of objects that need to be deleted and remove them.

}

}

NSMutableSet *objects = [self.managedObject valueForKey:propName];

NSMutableArray *toDelete = [NSMutableArray array]; for (NSManagedObject *oneObject in objects) {

if ([oneObject isDeleted]) [toDelete addObject:oneObject];

} for (NSManagedObject *oneObject in toDelete) {

}

[objects removeObject:oneObject]; NSError *error; if (![self.managedObject.managedObjectContext save:&error])

NSLog(@"Error saving: %@", [error localizedDescription]);

}
Share: