8

あなたが助けてくれることを願っています。今日のサポートをアプリに追加します。これは、MagicalRecord https://github.com/magicalpanda/MagicalRecordを使用してすべての CoreData のものを管理します。

今日の拡張機能にデータを表示する方法を理解しようとして、髪を引き裂いています。

ここで概説されているようにアプリグループを有効にしましたhttp://blog.sam-oakley.co.uk/post/92323630293/sharing-core-data-between-app-and-extension-in-ios-8ただし、すべてのドキュメントと私が読んでいる StackOverflow の投稿は、CoreData を直接使用することに関連しています。MagicalRecord はあなたのために多くの大変な作業をしてくれます。このプロジェクトの最初はまったくの初心者だったので、MagicalRecord を使用しました。次のようなもの:

Core Data スタックを初期化する場所で、次のように、persistentStoreCoordinator にストアを追加します。

[persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
URL:storeURL options:options error:&error]

以前の storeURL の値 (通常は NSDocumentDirectory のどこか) を、共有アプリ グループ フォルダーに含まれる場所に変更するだけです。これを行うには、

containerURLForSecurityApplicationGroupIdentifier: NSURL *directory =
[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"group.YourGroupName"];
NSURL *storeURL = [directory 
URLByAppendingPathComponent:@"YourAppName.sqlite"];

... どこでどのように実装すればよいかわかりません。

appDelegate で行っているように、拡張機能で MagicalRecord スタックを設定するだけでよいと想像していましたが、もちろん失敗しています。

誰かが同様の状況にあり、これを進める方法に光を当てることができることを本当に望んでいます.

投稿するために必要なコードがあれば教えてください。

前もって感謝します

4

3 に答える 3

5

これが MagicalRecord の以前のバージョンで機能するかどうかはわかりませんが、2.2 の時点では、ストア名として最終的な URL を渡すことができます:

NSFileManager *fileManager = [[NSFileManager alloc] init];

NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yellow"];
NSURL *pathToStore = [directory URLByAppendingPathComponent:kMagicalRecordDefaultStoreFileName];

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)pathToStore];
于 2015-02-13T08:55:25.003 に答える
4

このスレッドに従うことで修正できたのと同じ問題がありました。https://github.com/magicalpanda/MagicalRecord/issues/858

最初に NSPersistentStore+MagicalRecord.m の次のメソッドを更新しました

- (NSURL *) MR_urlForStoreName:(NSString *)storeFileName
{
  NSFileManager *fileManager = [[NSFileManager alloc] init];

  NSURL *directory = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.yourIdentifier"];
  NSURL *pathToStore = [directory URLByAppendingPathComponent:storeFileName];

  return pathToStore;

// NSArray *paths = [NSArray arrayWithObjects:[self MR_applicationDocumentsDirectory], [self MR_applicationStorageDirectory], nil];
// NSFileManager *fm = [[NSFileManager alloc] init];
//

// for (NSString *path in paths) 
// {
// NSString *filepath = [path stringByAppendingPathComponent:storeFileName];
// if ([fm fileExistsAtPath:filepath])
// {
// return [NSURL fileURLWithPath:filepath];
// }
// }
//
// return [NSURL fileURLWithPath:[[self MR_applicationStorageDirectory] stringByAppendingPathComponent:storeFileName]];
}

次に、拡張機能内で、ビューに次のメソッドを追加しました。

- (void)viewDidLoad {
  [super viewDidLoad];
  [MagicalRecord setupCoreDataStackWithStoreNamed:<storeFileName>];
}
于 2014-09-29T14:17:24.837 に答える
3

変化する

[MagicalRecord setupCoreDataStackWithStoreNamed:@"Database"];

 - (void)setupCoreDataStack
{
     if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil)
     {
        return;
    }

    NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

    NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.yourgroup"];
    storeURL = [storeURL URLByAppendingPathComponent:@"Database.sqlite"];

    [psc MR_addSqliteStoreNamed:storeURL withOptions:nil];
    [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:psc];
    [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:psc];
}
于 2015-02-18T19:33:12.647 に答える