我将测试应用程序放在一起,从启用了Core Data的项目的命令行工具的起点开始非常直接。 ARC已启用。
它基本上创建一个实体,保存它,从上下文中检索所有实体并删除它们。
我不明白,为什么内存使用量会增长和增长? 谢谢你的澄清。
这是测试程序:
#import "Message.h"
NSManagedObjectModel *managedObjectModel(void);
NSManagedObjectContext *managedObjectContext(void);
int main (int argc, const char * argv[])
{
@autoreleasepool {
while(true){
// Create the managed object context
NSManagedObjectContext *context = managedObjectContext();
Message *message = [NSEntityDescription
insertNewObjectForEntityForName:@"Message"
inManagedObjectContext:context];
message.string = @"A string";
// Save the managed object context
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
// Load and delete object from context
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Message *message in fetchedObjects) {
NSLog(@"String is: %@", message.string);
[context deleteObject: message];
[context save: &error];
}
sleep(0.005);
}
}
return 0;
}
NSManagedObjectModel *managedObjectModel() {
static NSManagedObjectModel *model = nil;
if (model != nil) {
return model;
}
NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
path = [path stringByDeletingPathExtension];
NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"momd"]];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return model;
}
NSManagedObjectContext *managedObjectContext() {
static NSManagedObjectContext *context = nil;
if (context != nil) {
return context;
}
@autoreleasepool {
context = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];
[context setPersistentStoreCoordinator:coordinator];
NSString *STORE_TYPE = NSSQLiteStoreType;
NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
path = [path stringByDeletingPathExtension];
NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];
NSError *error;
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];
if (newStore == nil) {
NSLog(@"Store Configuration Failure %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
}
return context;
}
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Message : NSManagedObject
@property (nonatomic, retain) NSString * string;
@end
#import "Message.h"
@implementation Message
@dynamic string;
@end
答案 0 :(得分:0)
你有这个
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
从未发布,你应该尝试将其初始化为自动释放
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];