截断的核心数据 NSData 对象

发布于 2024-08-18 22:02:14 字数 1767 浏览 7 评论 0原文

我将双精度数组保存在 NSData* 对象中,该对象作为二进制属性保留在 Core Data (SQLite) 数据模型中。我这样做是为了在 iPhone 应用程序中存储用于绘图的采样数据。有时,当二进制对象中有超过 300 个双精度数时,并非所有双精度数都会保存到磁盘。当我退出并重新启动我的应用程序时,可能只有 25 个数据点保留下来,最多可能有 300 个。

使用带有 synchronous = FULL 的 NSSQLitePragmasOption ,这可能会产生影响。很难说,因为错误是间歇性的。

鉴于使用 synchronous = FULL 导致的性能问题的警告,我正在寻求建议和指示。

谢谢。

[[编辑:这里是代码。]]

-addToCache 的(尚未实现)意图:是将每个新数据添加到缓存,但仅定期刷新(故障?)数据对象。

来自数据.m

@dynamic dataSet; // NSData * attribute of Data entity

 - (void) addDatum:(double_t)datum
    {
    DLog(@"-[Data addDatum:%f]", datum);
    [self addToCache:datum];
    }

- (void) addToCache:(double_t)datum
    {
    if (cache == nil)
        {
        cache = [NSMutableData dataWithData:[self dataSet]];
        [cache retain];
        }
    [cache appendBytes:&datum length:sizeof(double_t)];
    DLog(@"-[Data addToCache:%f] ... [cache length] = %d; cache = %p", datum, [cache length], cache);
    [self flushCache];
    }

- (void) wrapup
    {
    DLog(@"-[Data wrapup]");
    [self flushCache];
    [cache release];
    cache = nil;
    DLog(@"[self isFault] = %@", [self isFault] ? @"YES" : @"NO"); // [self isFault] is always NO.
    }

- (void) flushCache
    {
    DLog(@"flushing cache to store");
    [self setDataSet:cache];
    DLog(@"-[Data flushCache:] [[self dataSet] length] = %d", [[self dataSet] length]);
    }

- (double*) bytes
    {
    return (double*)[[self dataSet] bytes];
    }

- (NSInteger) count
    {
    return [[self dataSet] length]/sizeof(double);
    }

- (void) dump
    {
    ALog(@"Dump Data");
    NSInteger numDataPoints = [self count];
    double *data = (double*)[self bytes];
    ALog(@"numDataPoints = %d", numDataPoints);
    for (int i = 0; i 

I am saving arrays of doubles in an NSData* object that is persisted as a binary property in a Core Data (SQLite) data model. I am doing this to store sampled data for graphing in an iPhone app. Sometimes when there are more than 300 doubles in the binary object not all the doubles are getting saved to disk. When I quit and relaunch my app there may be as few as 25 data points that have persisted or as many as 300.

Using NSSQLitePragmasOption with synchronous = FULL and this may be making a difference. It is hard to tell, as bug is intermittent.

Given the warnings about performance problems as a result of using synchronous = FULL, I am seeking advice and pointers.

Thanks.

[[Edit: here is code.]]

The (as yet unrealized) intent of -addToCache: is to add each new datum to the cache but only flush (fault?) Data object periodically.

From Data.m


@dynamic dataSet; // NSData * attribute of Data entity

 - (void) addDatum:(double_t)datum
    {
    DLog(@"-[Data addDatum:%f]", datum);
    [self addToCache:datum];
    }

- (void) addToCache:(double_t)datum
    {
    if (cache == nil)
        {
        cache = [NSMutableData dataWithData:[self dataSet]];
        [cache retain];
        }
    [cache appendBytes:&datum length:sizeof(double_t)];
    DLog(@"-[Data addToCache:%f] ... [cache length] = %d; cache = %p", datum, [cache length], cache);
    [self flushCache];
    }

- (void) wrapup
    {
    DLog(@"-[Data wrapup]");
    [self flushCache];
    [cache release];
    cache = nil;
    DLog(@"[self isFault] = %@", [self isFault] ? @"YES" : @"NO"); // [self isFault] is always NO.
    }

- (void) flushCache
    {
    DLog(@"flushing cache to store");
    [self setDataSet:cache];
    DLog(@"-[Data flushCache:] [[self dataSet] length] = %d", [[self dataSet] length]);
    }

- (double*) bytes
    {
    return (double*)[[self dataSet] bytes];
    }

- (NSInteger) count
    {
    return [[self dataSet] length]/sizeof(double);
    }

- (void) dump
    {
    ALog(@"Dump Data");
    NSInteger numDataPoints = [self count];
    double *data = (double*)[self bytes];
    ALog(@"numDataPoints = %d", numDataPoints);
    for (int i = 0; i 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-08-25 22:02:14

我试图获得行为,就好像我的核心数据实体可以具有 NSMutableData 属性一样。为此,我的 NSManagedObject(称为 Data)有一个 NSData 属性一个 NSMutableData ivar。我的应用程序从传感器获取样本数据并将每个数据点附加到数据集 - 这就是我需要这种设计的原因。

将每个新数据点附加到 NSMutableData,然后将 NSData 属性设置为 NSMutableData。

我怀疑因为 NSData 指针没有改变(尽管它的内容改变了),所以 Core Data 没有意识到变化量。对 NSManagedObjectContext 调用 -hasChanged 显示发生了更改,调用 -updatedObjects 甚至将 Data 对象列出为已更改。但正在写入的实际数据似乎已被截断(有时)。

为了解决这个问题,我稍微改变了一些事情。新数据点仍然附加到 NSMutableData 但是 NSData 属性仅在采样完成时设置。这意味着崩溃有可能导致数据被截断 - 但在大多数情况下,这种解决方法似乎已经解决了问题。

买者自负:该错误总是间歇性的,因此它可能仍然存在 - 但更难显现出来。

I was trying to get behavior as if my Core Data entity could have an NSMutableData attribute. To do this my NSManagedObject (called Data) had an NSData attribute and an NSMutableData ivar. My app takes sample data from a sensor and appends each data point to the data set - this is why I needed this design.

On each new data point was appended to the NSMutableData and then the NSData attribute was set to the NSMutableData.

I suspect that because the NSData pointer wasn't changing (though its content was), that Core Data did not appreciate the amount of change. Calling -hasChanged on the NSManagedObjectContext showed that there had been changes, and calling -updatedObjects even listed the Data object as having changed. But the actual data that was being written seems to have been truncated (sometimes).

To work around this I changed things slightly. New data points are still appended to NSMutableData but NSData attribute is only set when sampling is completed. This means that there is a chance that a crash might result in truncated data - but for the most part this work around seems to have solved the problem.

Caveat emptor: the bug was always intermittent, so it is possible that is still there - but just harder to manifest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文