Add object detection, tracking, mobile notifications, and search to any security camera.
less crashy
roryclear7910ee38bdd1
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.mindex 704ad93..402b4c7 100644--- a/clearcam/FileServer.m+++ b/clearcam/FileServer.m@@ -581,71 +581,78 @@return @[];}- __block NSArray *copiedSegments = @[];+ __block NSArray *segmentObjectIDs = @[];[context performBlockAndWait:^{NSError *error = nil;-- // Fetch the SegmentEntity that matches the given URLNSFetchRequest *segmentFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SegmentEntity"];segmentFetchRequest.predicate = [NSPredicate predicateWithFormat:@"url == %@", url];- copiedSegments = [context executeFetchRequest:segmentFetchRequest error:&error];+ NSArray *segments = [context executeFetchRequest:segmentFetchRequest error:&error];if (error) {NSLog(@"Failed to fetch segments for URL %@: %@", url, error.localizedDescription);return;}- if (copiedSegments.count == 0) {+ if (segments.count == 0) {NSLog(@"No segments found for URL %@", url);return;}++ // Store object IDs instead of NSManagedObject references+ segmentObjectIDs = [segments valueForKey:@"objectID"];}];- // Process frames outside of performBlockAndWait to avoid blocking Core DataNSMutableArray *framesForURL = [NSMutableArray array];- for (NSManagedObject *segment in copiedSegments) {- NSArray *frames = [[segment valueForKey:@"frames"] array]; // Copy frames-- for (NSManagedObject *frame in frames) {- double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];- double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];- int res = [[frame valueForKey:@"res"] intValue];-- NSMutableArray *squareDicts = [NSMutableArray array];- NSArray *squares = [[frame valueForKey:@"squares"] array]; // Copy squares-- for (NSManagedObject *square in squares) {- double originX = [[square valueForKey:@"originX"] doubleValue];- double originY = [[square valueForKey:@"originY"] doubleValue];- double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];- double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];- int classIndex = [[square valueForKey:@"classIndex"] intValue];+ // Use a new context tied to the current queue+ NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];+ bgContext.parentContext = context;++ [bgContext performBlockAndWait:^{+ for (NSManagedObjectID *segmentID in segmentObjectIDs) {+ NSManagedObject *segment = [bgContext objectWithID:segmentID];+ NSArray *frames = [[segment valueForKey:@"frames"] array];++ for (NSManagedObject *frame in frames) {+ double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];+ double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];+ int res = [[frame valueForKey:@"res"] intValue];++ NSMutableArray *squareDicts = [NSMutableArray array];+ NSArray *squares = [[frame valueForKey:@"squares"] array];++ for (NSManagedObject *square in squares) {+ double originX = [[square valueForKey:@"originX"] doubleValue];+ double originY = [[square valueForKey:@"originY"] doubleValue];+ double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];+ double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];+ int classIndex = [[square valueForKey:@"classIndex"] intValue];++ NSDictionary *squareDict = @{+ @"originX": @(originX),+ @"originY": @(originY),+ @"bottomRightX": @(bottomRightX),+ @"bottomRightY": @(bottomRightY),+ @"classIndex": @(classIndex)+ };++ [squareDicts addObject:squareDict];+ }- NSDictionary *squareDict = @{- @"originX": @(originX),- @"originY": @(originY),- @"bottomRightX": @(bottomRightX),- @"bottomRightY": @(bottomRightY),- @"classIndex": @(classIndex)+ NSDictionary *frameDict = @{+ @"url": url,+ @"frame_timeStamp": @(frameTimeStamp),+ @"aspect_ratio": @(aspectRatio),+ @"res": @(res),+ @"squares": squareDicts};- [squareDicts addObject:squareDict];+ [framesForURL addObject:frameDict];}-- NSDictionary *frameDict = @{- @"url": url, // Include the segment's URL- @"frame_timeStamp": @(frameTimeStamp),- @"aspect_ratio": @(aspectRatio),- @"res": @(res),- @"squares": squareDicts- };-- [framesForURL addObject:frameDict];}- }+ }];NSLog(@"Fetched and processed %lu frames for URL %@", (unsigned long)framesForURL.count, url);