How to delete all records in Coredata while keeping the storage type NSInMemoryStoreType. Want it to happen in XCTest

I am trying to unit test the main helper data classes, so in my setUp function, I create a basic data db in memory by creating NSManagedObjectContext

with this code:

+ (NSManagedObjectContext *)managedObjectContextForTesting {
    static NSManagedObjectContext *_managedObjectContext = nil;
    if (_managedObjectContext == nil) {
        NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];

        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Buddyup" withExtension:@"momd"];
        NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];

        [psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil];

        [moc setPersistentStoreCoordinator:psc];

        _managedObjectContext = moc;
    }
    return _managedObjectContext;
}

      

I can only find how to delete master data records if saved to disk. The usual answer is you need to delete persistent storage. So I tried to follow and tried:

+(void)flushDataBase
{
    NSError * error;

    NSManagedObjectContext * moc = [ManagedObjectContextTesting managedObjectContextForTesting];
    NSPersistentStoreCoordinator* psc = [moc persistentStoreCoordinator];
    [moc lock];
    for(NSPersistentStore *store in [psc persistentStores]) {
        [psc removePersistentStore:store error:nil];

         //this line will throw an error because store.URL looks something like "memory://12345"
        [[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]; 

    }
    [psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error]; 
    [moc unlock];

}

      

flushDataBase

called in the tearDown method. It doesn't work because when it hits the removeItemAtPath line it throws an error   error: -[UserCoreDataTests testGetUserWithId_ReturnsNothing] : *** -[NSFileManager fileSystemRepresentationWithPath:]: nil or empty path argument

This is because store.URL is looking for something "memory: // 12345" instead of "User: // directory". This means that the persistence storage url points to some place in memory. So how do you delete a storage item in RAM?

+3
ios core-data persistent-storage xctest


source to share


No one has answered this question yet

See similar questions:

15
How do I delete all objects from my persistent storage in Core Data?

or similar:

229
Delete / Reset All Entries in Core Data?
five
Founding Document-Based Application with Global Persistent Storage
4
NSStoreModelVersionHash error in Core-Data migration
3
Calling mergeChangesFromContextDidSaveNotification: Not Triggering mocObjectsDidChangeNotification in the main context
2
_PFFaultHandlerLookupRow error while saving child context
1
Managing Multiple NSPersistentStores with PersistentStoreCoodinator
1
Testing iOS Modules Using Master Data
0
Xcode 5 does not generate core data template functionality in AppDelegate
0
Synchronizing kernel data on iOS devices
0
NSPersistentStoreCoordinator does not have persistent stores (cannot be opened)



All Articles
Loading...
X
Show
Funny
Dev
Pics