如何在removeAllObjects中使用RLMArray
我收到'RLMException', reason: 'Attempting to mutate a readOnly RLMArray'错误。

#import "ViewController.h"
#import <Realm/Realm.h>
#import "Person.h"

@interface ViewController ()
@property RLMArray *list;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _list = [Person allObjects];
    NSLog(@"%@", _list);

}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

- (IBAction)addPerson:(id)sender {
    Person *human = [[Person alloc] init];
    human.name = @"John";
    human.title = @"Wizard";

    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    [realm addObject:human];
    [realm commitWriteTransaction];

}

- (IBAction)removePerson:(id)sender {
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    [_list removeAllObjects];
    [realm commitWriteTransaction];
}
@end

最佳答案

我不是100%积极,但我认为这应该有效。

- (IBAction)removePerson:(id)sender {
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    [realm deleteObjects:_list];
    [realm commitWriteTransaction];
}

关于ios - 如何在“RLMArray”中使用“removeAllObjects”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26535586/

10-11 19:59