来自调试器的消息:由于内存压力IOS而终止?

时间:2015-09-29 10:20:13

标签: ios objective-c iphone nsarray

在服务器端数据中我得到了17000个用户名。当我得到1000个用户名时,Xcode正在崩溃,从调试器发出这些错误消息:由于内存压力而终止请帮助我如何获得17000用户名数据如何处理没有内存presser.i在实际设备中遇到这些问题。

为什么我在app delegate中使用数组我需要在这么多视图控制器中使用这些17000个用户名

I am declare array
AppDelegate.h
@property (nonatomic,retain) NSMutableArray *userNamesGettingArrayObj;

I am declare string
ModelClass.h
@property (nonatomic,strong) NSString *nameString;

ModelClass.m
// i am getting server data in these dictionary object
NSDictionary *dictobj=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
        for (int i = 0; i<=[[dictobj valueForKey:@"name"] count]-1;i++)
        {
            _nameString=[[dictobj valueForKey:@"name"]objectAtIndex:i];
            [delegate.userNamesGettingArrayObj addObject:_nameString];
        }


viewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return [delegate.userNamesGettingArrayObj count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_tableViewObj == tableView) {
        static NSString *ide=@"ide";
        UITableViewCell *cell=[_tableViewObj dequeueReusableCellWithIdentifier:ide];
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ide];
        }

            cell.textLabel.text=[delegate.userNamesGettingArrayObj objectAtIndex:indexPath.row];

        return cell;
    }
}

4 个答案:

答案 0 :(得分:0)

不要同时从服务器获取那么多数据,当你的表到达列表末尾时,使用加载更多按钮或自动点击功能。

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;{
      if(nearByPlacesTableView.contentOffset.y >= (nearByPlacesTableView.contentSize.height - nearByPlacesTableView.bounds.size.height)) {
        if (isPageRefresing) {
           // [self performSelector:@selector(getRecords:) withObject:nextPageTokenString afterDelay:0.0f];
        [self performSelectorInBackground:@selector(getRecords:) withObject:nextPageTokenString];
      }
   }
 }

// HTTP Utility类      - (void)getRecords:(NSString *)token {

NSString *serverUrl;


if ([self.headStr isEqualToString:@"Nearby"])
{
    serverUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=%@&key=AIzaSyCd2",token];
}else{
    serverUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken=%@&key=AIzaSyCd2",token];
}

//Create URL and Request
NSURL * url = [NSURL URLWithString:serverUrl];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSURLResponse * response;
NSError * error = nil;
//Send Request
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error == nil)
{
    NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *nextpageArr = [NSMutableArray new];
    nextpageArr = [[json valueForKey:@"results"] mutableCopy];
    nextPageTokenString=[json valueForKey:@"next_page_token"];

    for (id dict in  [json valueForKey:@"results"])
    {
        NSMutableDictionary *mutableDict;
        if ([dict isKindOfClass:[NSDictionary class]]) {
            mutableDict=[dict mutableCopy];
        }
        else{
            mutableDict=dict;
        }
        [mutableDict setValue:@"0" forKey:@"checked"];
        [mutableDict setValue:@"0" forKey:@"is_favourite"];
        [nextpageArr addObject:mutableDict];
    }

    NSString *status=[json valueForKey:@"status"];
    if ([status isEqualToString:@"INVALID_REQUEST"]) {
        isPageRefresing=NO;
    }else if ([status isEqualToString:@"OK"]){
        isPageRefresing=YES;
    }
    if (nextpageArr.count) {
        [self.nearbyVenues addObjectsFromArray:nextpageArr];
    }

    [nearByPlacesTableView reloadData];
    [SVProgressHUD dismiss];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet" message:@"Please check your internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
  }
 }

答案 1 :(得分:0)

我怀疑你反复重复使用valueForKey可能是问题所在。无论是否是问题,它都是如此低效,它会伤害我的眼睛。大约快100倍:

NSArray* names = dictObj [@"name"];
NSMutableArray* userNames = delegate.userNamesGettingArrayObj;

for (NSString* nameString in names)
    [names addObject:nameString];

除非您的代码出现问题,否则17,000个名称应该没有任何问题。

valueForKey是一种高级方法,通常完全不适合处理JSON或访问存储在字典中的任何内容。除非你有充分的理由(如果被问及可以解释),请使用objectForKey或仅使用[@&#34; someKey&#34;]。

答案 2 :(得分:0)

必须尝试?

让响应dict golable and cell.textlable.text = [dictobj valueForKey:@&#34; name&#34;] objectAtIndex:indexpath.row];

答案 3 :(得分:0)

//我将所有数据存储到我使用这些代码的时间

delegate.userNamesGettingArrayObj=[[[NSArray arrayWithObject:dictobj]valueForKey:@"name"]objectAtIndex:0];