我有一个小问题,每当我从详细信息视图控制器回到主视图控制器时,我将重复数据添加到REALM数据库。我还试图替换viewDidLoad中的网络代码,但是我收到此错误 *由于未捕获的异常'RLMException'终止应用程序,原因:'索引2超出界限*。以下是我的代码:< / p>
#import "HomeTVC.h"
#import "facebook.h"
#import "HomeTVCell.h"
#import "MBProgressHUD.h"
#import "PageVideosCVC.h"
#import "HomePageList.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface HomeTVC ()<UITableViewDataSource, UITableViewDelegate>
{
NSDictionary *userPageLikesParams;
NSMutableArray *pagesInfo;
NSArray *pagesInfoFromRealm;
facebook *fb;
HomePageList *homePageList;
}
@end
@implementation HomeTVC
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
// Add HUD
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
userPageLikesParams = @{@"fields": @"about,name,created_time,picture",@"limit": @"10"} ;
fb = [[facebook alloc] init];
[fb getUserPagelikes:userPageLikesParams completionHandler:^(NSDictionary *pagesResult) {
if (pagesResult != nil) {
[pagesInfo addObjectsFromArray:[pagesResult valueForKeyPath:@"data"]];
// pagesInfo = pagesResult[@"data"];
// Delete all available data in REALM
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];
[realm beginWriteTransaction];
for(NSDictionary *pageInfoToSaveInRealm in pagesInfo){
HomePageList *homePages = [[HomePageList alloc] init];
homePages.pageID = pageInfoToSaveInRealm[@"id"];
homePages.pageName = pageInfoToSaveInRealm[@"name"];
homePages.pageProfilePic = [pageInfoToSaveInRealm valueForKeyPath:@"picture.data.url"];
[realm addObject:homePages];
dispatch_async(dispatch_get_main_queue(), ^{
RLMRealm *realmMainThread = [RLMRealm defaultRealm];
RLMResults *pagesInformation = [HomePageList allObjectsInRealm:realmMainThread];
pagesInfoFromRealm = pagesInformation;
NSLog(@"ayam : %d", (int)pagesInfoFromRealm.count);
[self.tableView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}
[realm commitWriteTransaction];
}
} failure:^(NSError *error) {
if(error) {
pagesInfoFromRealm = [HomePageList allObjects];
[self.tableView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
}];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
pagesInfo = [NSMutableArray array];
// [facebook currentFBAccessToken];
// [facebook getFBUserInfo];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (pagesInfoFromRealm == nil) {
return 0;
} else {
NSLog(@"table count : %d", (int)pagesInfoFromRealm.count);
return pagesInfoFromRealm.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
HomeTVCell *cell = (HomeTVCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// NSLog(@"pagesInfoFromRealm : %@", pagesInfoFromRealm);
homePageList = pagesInfoFromRealm[[indexPath item]];
NSURL *imageURL = [NSURL URLWithString:homePageList.pageProfilePic];
// cache the image using sdwebimage
cell.pageProfilePic.layer.backgroundColor=[[UIColor clearColor] CGColor];
cell.pageProfilePic.layer.borderWidth = 2.0;
cell.pageProfilePic.layer.masksToBounds = YES;
cell.pageProfilePic.layer.borderColor=[[UIColor whiteColor] CGColor];
cell.pageProfilePic.layer.cornerRadius= 30.0;
[cell.pageProfilePic sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
cell.pageName.text = homePageList.pageName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PageVideosCVC *pageVideo = [sb instantiateViewControllerWithIdentifier:@"videoDetails"];
homePageList = pagesInfoFromRealm[indexPath.row];
NSLog(@"pagesInfoFromRealm array count: %d", (int)pagesInfoFromRealm.count);
NSLog(@"homepagelist data: %@", pagesInfoFromRealm[indexPath.row]);
pageVideo.pageID = homePageList[@"pageID"];
pageVideo.pageName = homePageList[@"pageName"];
[self presentViewController:pageVideo animated:YES completion:nil];
}
@end
提前致谢。
答案 0 :(得分:2)
您正在viewDidAppear:
添加数据。每次显示视图时都会调用该方法。因此,当用户从详细视图返回到此视图时,也会调用它。
相反,请将其移至viewDidLoad
或将代码移至另一个方法,并仅在需要时调用它。
如果您确实需要在viewDidAppear
中使用该代码,并假设您已将pageId
标记为HomePageList
的主键,则可以执行addOrUpdateObject:
而不是addObject:
。这将确保如果对象已存在于Realm中,则仅更新它不会重复。
答案 1 :(得分:0)
要确保只执行一次此操作,您可以将操作移至isMovingToParent
检查,这是一种特殊的BOOL
方法,只能在UIViewController
转换期间使用:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ( [self isMovingToParentViewController]) {
// Action
}
}