Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

less crashy maybe

roryclear020ba23eae73
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 2a2ea29..9a19615 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -79,92 +79,92 @@
}
- (NSArray *)fetchAndProcessSegmentsFromCoreDataForDateParam:(NSString *)dateParam context:(NSManagedObjectContext *)context {
- // Create a temporary array for the segment dictionaries
- NSMutableArray *tempSegmentDicts = [NSMutableArray array];
- NSError *error = nil;
- // Fetch the DayEntity for the given date
- NSFetchRequest *dayFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"DayEntity"];
- dayFetchRequest.predicate = [NSPredicate predicateWithFormat:@"date == %@", dateParam];
+ __block NSMutableArray *tempSegmentDicts = [NSMutableArray array];
- NSArray *fetchedDays = [context executeFetchRequest:dayFetchRequest error:&error];
+ [context performBlockAndWait:^{
+ NSError *error = nil;
- if (error) {
- NSLog(@"Failed to fetch DayEntity: %@", error.localizedDescription);
- return tempSegmentDicts;
- }
+ // Fetch the DayEntity for the given date
+ NSFetchRequest *dayFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"DayEntity"];
+ dayFetchRequest.predicate = [NSPredicate predicateWithFormat:@"date == %@", dateParam];
- if (fetchedDays.count == 0) {
- NSLog(@"No DayEntity found for date %@",dateParam);
- return tempSegmentDicts;
- }
+ NSArray *fetchedDays = [context executeFetchRequest:dayFetchRequest error:&error];
+
+ if (error) {
+ NSLog(@"Failed to fetch DayEntity: %@", error.localizedDescription);
+ return;
+ }
+
+ if (fetchedDays.count == 0) {
+ NSLog(@"No DayEntity found for date %@", dateParam);
+ return;
+ }
+
+ NSManagedObject *dayEntity = fetchedDays.firstObject;
+ NSOrderedSet *segments = [dayEntity valueForKey:@"segments"];
- NSManagedObject *dayEntity = fetchedDays.firstObject;
- NSOrderedSet *segments = [dayEntity valueForKey:@"segments"];
+ for (NSManagedObject *segment in segments) {
+ NSString *url = [segment valueForKey:@"url"];
+ double timeStamp = [[segment valueForKey:@"timeStamp"] doubleValue];
+ double duration = [[segment valueForKey:@"duration"] doubleValue];
- for (NSManagedObject *segment in segments) {
- NSString *url = [segment valueForKey:@"url"];
- double timeStamp = [[segment valueForKey:@"timeStamp"] doubleValue];
- double duration = [[segment valueForKey:@"duration"] doubleValue];
+ NSMutableArray *frameDicts = [NSMutableArray array];
- NSMutableArray *frameDicts = [NSMutableArray array];
+ NSOrderedSet *frames = [segment valueForKey:@"frames"];
- // Fetch the ordered frames
- NSOrderedSet *frames = [segment valueForKey:@"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];
- 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];
- NSMutableArray *squareDicts = [NSMutableArray array];
+ NSOrderedSet *squares = [frame valueForKey:@"squares"];
- // Fetch the ordered squares within this frame
- NSOrderedSet *squares = [frame valueForKey:@"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];
- 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 = @{
+ @"frame_timeStamp": @(frameTimeStamp),
+ @"aspect_ratio": @(aspectRatio),
+ @"res": @(res),
+ @"squares": squareDicts
};
- [squareDicts addObject:squareDict];
+ [frameDicts addObject:frameDict];
}
- NSDictionary *frameDict = @{
- @"frame_timeStamp": @(frameTimeStamp),
- @"aspect_ratio": @(aspectRatio),
- @"res": @(res),
- @"squares": squareDicts
+ NSDictionary *segmentDict = @{
+ @"url": url,
+ @"timeStamp": @(timeStamp),
+ @"duration": @(duration),
+ @"frames": frameDicts
};
- [frameDicts addObject:frameDict];
+ [tempSegmentDicts addObject:segmentDict];
}
- NSDictionary *segmentDict = @{
- @"url": url,
- @"timeStamp": @(timeStamp),
- @"duration": @(duration),
- @"frames": frameDicts
- };
-
- [tempSegmentDicts addObject:segmentDict];
- }
-
- // Log the results
- NSLog(@"rory length of init segments = %lu", (unsigned long)tempSegmentDicts.count);
- NSLog(@"Fetched and processed %lu segments for date %@", (unsigned long)tempSegmentDicts.count,dateParam);
+ NSLog(@"Fetched and processed %lu segments for date %@", (unsigned long)tempSegmentDicts.count, dateParam);
+ }];
return tempSegmentDicts;
}
+
- (void)startHTTPServerWithBasePath:(NSString *)basePath {
@try {
signal(SIGPIPE, SIG_IGN);