mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(file-system): append, appendText & createFile (#10285)
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
D004031022F781A50089EAD8 /* NSString+Async.m in Sources */ = {isa = PBXBuildFile; fileRef = D004030E22F781A50089EAD8 /* NSString+Async.m */; };
|
||||
D004031322FA27D60089EAD8 /* NSData+Async.h in Headers */ = {isa = PBXBuildFile; fileRef = D004031122FA27D60089EAD8 /* NSData+Async.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
D004031422FA27D60089EAD8 /* NSData+Async.m in Sources */ = {isa = PBXBuildFile; fileRef = D004031222FA27D60089EAD8 /* NSData+Async.m */; };
|
||||
F11DE2162A02F39A00B70DC5 /* NSFileHandle+Async.m in Sources */ = {isa = PBXBuildFile; fileRef = F11DE2152A02F39A00B70DC5 /* NSFileHandle+Async.m */; };
|
||||
F11DE2182A02F3B500B70DC5 /* NSFileHandle+Async.h in Headers */ = {isa = PBXBuildFile; fileRef = F11DE2172A02F3B500B70DC5 /* NSFileHandle+Async.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
F915D3551EC9EF5E00071914 /* TNSProcess.m in Sources */ = {isa = PBXBuildFile; fileRef = F915D3531EC9EF5E00071914 /* TNSProcess.m */; };
|
||||
F915D3561EC9EF5E00071914 /* TNSProcess.h in Headers */ = {isa = PBXBuildFile; fileRef = F915D3541EC9EF5E00071914 /* TNSProcess.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
F98F5CB31CD0EFEA00978308 /* TNSWidgets.h in Headers */ = {isa = PBXBuildFile; fileRef = F98F5CB21CD0EFEA00978308 /* TNSWidgets.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -51,6 +53,8 @@
|
||||
D004030E22F781A50089EAD8 /* NSString+Async.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+Async.m"; sourceTree = "<group>"; };
|
||||
D004031122FA27D60089EAD8 /* NSData+Async.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+Async.h"; sourceTree = "<group>"; };
|
||||
D004031222FA27D60089EAD8 /* NSData+Async.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+Async.m"; sourceTree = "<group>"; };
|
||||
F11DE2152A02F39A00B70DC5 /* NSFileHandle+Async.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSFileHandle+Async.m"; sourceTree = "<group>"; };
|
||||
F11DE2172A02F3B500B70DC5 /* NSFileHandle+Async.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSFileHandle+Async.h"; sourceTree = "<group>"; };
|
||||
F915D3531EC9EF5E00071914 /* TNSProcess.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TNSProcess.m; path = ../TNSProcess.m; sourceTree = "<group>"; };
|
||||
F915D3541EC9EF5E00071914 /* TNSProcess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TNSProcess.h; path = ../TNSProcess.h; sourceTree = "<group>"; };
|
||||
F98F5CAF1CD0EFEA00978308 /* TNSWidgets.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TNSWidgets.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -121,6 +125,8 @@
|
||||
B8E76F5D212C3134009CFCE2 /* UIView+PassThroughParent.m */,
|
||||
D004031122FA27D60089EAD8 /* NSData+Async.h */,
|
||||
D004031222FA27D60089EAD8 /* NSData+Async.m */,
|
||||
F11DE2172A02F3B500B70DC5 /* NSFileHandle+Async.h */,
|
||||
F11DE2152A02F39A00B70DC5 /* NSFileHandle+Async.m */,
|
||||
);
|
||||
path = TNSWidgets;
|
||||
sourceTree = "<group>";
|
||||
@@ -147,6 +153,7 @@
|
||||
D004031322FA27D60089EAD8 /* NSData+Async.h in Headers */,
|
||||
F98F5CCB1CD0F09E00978308 /* UIImage+TNSBlocks.h in Headers */,
|
||||
B8E76F52212C2DA2009CFCE2 /* NSObject+Swizzling.h in Headers */,
|
||||
F11DE2182A02F3B500B70DC5 /* NSFileHandle+Async.h in Headers */,
|
||||
B8E76F5A212C2F4E009CFCE2 /* UIView+PropertyBag.h in Headers */,
|
||||
D004030F22F781A50089EAD8 /* NSString+Async.h in Headers */,
|
||||
8B7321CF1D097ECD00884AC6 /* TNSLabel.h in Headers */,
|
||||
@@ -252,6 +259,7 @@
|
||||
files = (
|
||||
8B7321D01D097ECD00884AC6 /* TNSLabel.m in Sources */,
|
||||
F915D3551EC9EF5E00071914 /* TNSProcess.m in Sources */,
|
||||
F11DE2162A02F39A00B70DC5 /* NSFileHandle+Async.m in Sources */,
|
||||
F98F5CCC1CD0F09E00978308 /* UIImage+TNSBlocks.m in Sources */,
|
||||
B8E76F53212C2DA2009CFCE2 /* NSObject+Swizzling.m in Sources */,
|
||||
D004031022F781A50089EAD8 /* NSString+Async.m in Sources */,
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// NSFileHandle+Async.h
|
||||
// TNSWidgets
|
||||
//
|
||||
// Created by Osei Fortune on 03/05/2023.
|
||||
// Copyright © 2023 Telerik A D. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSFileHandle (Async)
|
||||
|
||||
- (void)appendData:(nonnull NSData*) data
|
||||
completion:(void (^) (NSError*))callback;
|
||||
|
||||
+ (void)fileHandleWith:(NSString *)path data:(NSData *)data completion:(void (^)(NSFileHandle*,NSError*))callback;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// NSFileHandle+Async.m
|
||||
// TNSWidgets
|
||||
//
|
||||
// Created by Osei Fortune on 03/05/2023.
|
||||
// Copyright © 2023 Telerik A D. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSFileHandle+Async.h"
|
||||
|
||||
@implementation NSFileHandle(Async)
|
||||
|
||||
- (void)appendData:(NSData *)data completion:(void (^)(NSError*))callback {
|
||||
dispatch_queue_t asyncQueue = dispatch_queue_create("org.nativescript.TNSWidgets.fileHandle", NULL);
|
||||
dispatch_async(asyncQueue, ^(void) {
|
||||
NSError *error = nil;
|
||||
|
||||
if (@available(iOS 13.0, *)) {
|
||||
[self seekToEndReturningOffset:nil error:&error];
|
||||
[self writeData:data error:&error];
|
||||
} else {
|
||||
@try {
|
||||
[self seekToEndOfFile];
|
||||
[self writeData:data];
|
||||
} @catch (NSException *exception) {
|
||||
|
||||
NSMutableDictionary * info = [NSMutableDictionary dictionary];
|
||||
[info setValue:exception.name forKey:@"ExceptionName"];
|
||||
[info setValue:exception.reason forKey:@"ExceptionReason"];
|
||||
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
|
||||
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
|
||||
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
|
||||
|
||||
error = [[NSError alloc] initWithDomain:@"" code: 1 userInfo:info];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
callback(error);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
+ (void)fileHandleWith:(NSString *)path data:(NSData *)data completion:(void (^)(NSFileHandle*, NSError*))callback {
|
||||
dispatch_queue_t asyncQueue = dispatch_queue_create("org.nativescript.TNSWidgets.fileHandle", NULL);
|
||||
dispatch_async(asyncQueue, ^(void) {
|
||||
NSFileHandle* handle = [NSFileHandle fileHandleForWritingAtPath: path];
|
||||
NSError *error = nil;
|
||||
|
||||
if (@available(iOS 13.0, *)) {
|
||||
[handle seekToEndReturningOffset:nil error:&error];
|
||||
[handle writeData:data error:&error];
|
||||
} else {
|
||||
@try {
|
||||
[handle seekToEndOfFile];
|
||||
[handle writeData:data];
|
||||
} @catch (NSException *exception) {
|
||||
|
||||
NSMutableDictionary * info = [NSMutableDictionary dictionary];
|
||||
[info setValue:exception.name forKey:@"ExceptionName"];
|
||||
[info setValue:exception.reason forKey:@"ExceptionReason"];
|
||||
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
|
||||
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
|
||||
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
|
||||
|
||||
error = [[NSError alloc] initWithDomain:@"" code: 1 userInfo:info];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
callback(handle,error);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -22,3 +22,4 @@ FOUNDATION_EXPORT const unsigned char TNSWidgetsVersionString[];
|
||||
#import "TNSProcess.h"
|
||||
#import "NSString+Async.h"
|
||||
#import "NSData+Async.h"
|
||||
#import "NSFileHandle+Async.h"
|
||||
|
||||
Reference in New Issue
Block a user