iPhone の Cocoa で CoreData デバッグ引数をコンソールに出力するその他の方法
iPhone の Cocoa で CoreData デバッグ引数をコンソールに出力する方法
CoreData デバッグ引数は、CoreData の動作に関する詳細な情報を提供する強力なツールです。コンソールに出力することで、パフォーマンスのボトルネックを特定したり、データの破損を調査したり、オブジェクトグラフを可視化したりするのに役立ちます。
手順
- Xcode でプロジェクトを開きます。
- 対象となるファイルを開きます。
- デバッグ引数をコンソールに出力したい行を選択します。
- エディタの右側のマージンにあるブレークポイントインジケータをクリックします。
- 表示されたメニューから、「ログアクション」 > 「CoreData デバッグ」を選択します。
- デバッグ引数の種類を選択します。
- 「実行」をクリックします。
デバッグ引数の種類
- オブジェクトグラフ: オブジェクトグラフ全体を表示します。
- フェッチリクエスト: フェッチリクエストの実行に関する情報を表示します。
- 保存操作: 保存操作に関する情報を表示します。
例
// コンテキストを保存します。
NSError *error;
if (![context save:&error]) {
NSLog(@"Failed to save context: %@", error);
}
上記コードで、コンテキストの保存に失敗した場合、エラーメッセージとともにデバッグ情報を出力するには、次の手順を実行します。
NSLog
ステートメントの行を選択します。- ブレークポイントインジケータをクリックします。
- 「保存操作」を選択します。
注意事項
- デバッグ引数の出力は、パフォーマンスに影響を与える可能性があります。デバッグが必要ない場合は、無効にする必要があります。
- デバッグ引数の出力は、大量のデータを含む場合があり、コンソール画面が読み取りにくくなる可能性があります。
// AppDelegate.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <CoreData/CoreData.h>
@interface AppDelegate () <UIApplicationDelegate>
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// コアデータスタックを設定します。
[self setupCoreDataStack];
// ウィンドウを作成します。
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [self createRootViewController];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate.
// See the documentation for more information.
// コンテキストを保存します。
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Failed to save context: %@", error);
}
}
#pragma mark - Core Data Stack
- (void)setupCoreDataStack {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyModel" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSError *error;
NSURL *documentsDirectory = [[NSFileManager defaultManager] urlsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask error:&error];
NSURL *storeURL = [documentsDirectory[0] URLByAppendingPathComponent:@"MyDatabase.sqlite"];
if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Failed to add persistent store: %@", error);
abort();
}
self.managedObjectContext = [[NSManagedObjectContext alloc] initWithCoordinator:coordinator];
}
#pragma mark - Root View Controller
- (UIViewController *)createRootViewController {
// ルートビューコントローラを作成して初期化します。
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view.backgroundColor = [UIColor whiteColor];
// コンテキストを設定します。
rootViewController.managedObjectContext = self.managedObjectContext;
// ボタンを作成して、オブジェクトを作成するアクションを追加します。
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Create Object" forState:UIControlStateNormal];
[button addTarget:rootViewController action:@selector(createObject) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(20, 20, 100, 50);
[rootViewController.view addSubview:button];
return rootViewController;
}
@end
// ViewController.m
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// AppDelegate からコンテキストを取得します。
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.managedObjectContext = appDelegate.managedObjectContext;
}
- (void)createObject {
// 新しいオブジェクトを作成します。
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
// オブジェクトに値を設定します。
[object setValue:@"My Value" forKey:@"name"];
// コンテキストを保存します。
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Failed to save context: %@", error);
}
}
@end
このコードを実行すると、ボタンをクリックするたびに、新しいオブジェクトが作成され、CoreData デバッグ情報がコンソールに出力されます。
説明
AppDelegate
クラスは、CoreData スタックを設定します。ViewController
クラスは
(lldb) po [context debugDescription]
上記のコマンドは、現在のコンテキストのデバッグ情報をコンソールに出力します。
CocoaDebug ライブラリを使用する
CocoaDebug は、iOS アプリケーションのデバッグを容易にするオープンソースライブラリです。CocoaDebug を使用すると、CoreData デバッグ引数をコンソールに出力するための GUI ツールを使用できます。
iphone cocoa debugging